English 中文(简体)
CakePHP - Services
  • 时间:2024-09-17

CakePHP - Services


Previous Page Next Page  

This chapter deals with the information about the authentication process available in CakePHP.

Authentication

Authentication is the process of identifying the correct user. CakePHP supports three types of authentication.

    FormAuthenticate − It allows you to authenticate users based on form POST data. Usually, this is a login form that users enter information into. This is default authentication method.

    BasicAuthenticate − It allows you to authenticate users using Basic HTTP authentication

    DigestAuthenticate − It allows you to authenticate users using Digest HTTP authentication.

Example for FormAuthentication

Make changes in the config/routes.php file as shown in the following code.

config/routes.php

<?php
use CakeCorePlugin;
use CakeRoutingRouteBuilder;
use CakeRoutingRouter;
Router::defaultRouteClass( DashedRoute );
Router::scope( / , function (RouteBuilder $routes) {
   $routes->connect( /auth ,[ controller => Authexs , action => index ]);
   $routes->connect( /login ,[ controller => Authexs , action => login ]);
   $routes->connect( /logout ,[ controller => Authexs , action => logout ]);
   $routes->fallbacks( DashedRoute );
});
Plugin::routes();

Change the code of AppController.php file as shown in the following program.

src/Controller/AppController.php

<?php
namespace AppController;
use CakeControllerController;
use CakeEventEvent;
use CakeControllerComponentAuthComponent;
class AppController extends Controller {
   pubpc function initiapze() {
      parent::initiapze();
      $this->loadComponent( RequestHandler );
      $this->loadComponent( Flash );
         $this->loadComponent( Auth , [
             authenticate  => [
                Form  => [
                   fields  => [
                username  =>  username ,
                password  =>  password 
            ]
         ]
      ],
       loginAction  => [
          controller  =>  Authexs ,
          action  =>  login 
      ],
       loginRedirect  => [
          controller  =>  Authexs ,
          action  =>  index 
      ],
       logoutRedirect  => [
          controller  =>  Authexs ,
          action  =>  login 
      ]
   ]);
}
pubpc function beforeFilter(Event $event) {
      $this->Auth->allow([ index , view ]);
      $this->set( loggedIn , $this->Auth->user());
   }
}

Create AuthexsController.php file at src/Controller/AuthexsController.php. Copy the following code in the controller file.

src/Controller/AuthexsController.php

<?php
namespace AppController;
use AppControllerAppController;
use CakeORMTableRegistry;
use CakeDatasourceConnectionManager;
use CakeEventEvent;
use CakeAuthDefaultPasswordHasher;
class AuthexsController extends AppController {
   var $components = array( Auth );
   pubpc function index(){
   }
   pubpc function login(){
      if($this->request->is( post )) {
         $user = $this->Auth->identify();
         if($user){
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
         } else
         $this->Flash->error( Your username or password is incorrect. );
      }
   }
   pubpc function logout(){
      return $this->redirect($this->Auth->logout());
   } 
}
?>

Create a directory Authexs at src/Template and under that directory create a View file called login.php. Copy the following code in that file.

src/Template/Authexs/login.php

<?php
   echo $this->Form->create();
   echo $this->Form->control( username );
   echo $this->Form->control( password );
   echo $this->Form->button( Submit );
   echo $this->Form->end();
?>

Create another View file called logout.php. Copy the following code in that file.

src/Template/Authexs/logout.php

You are successfully logged out.

Create another View file called index.php. Copy the following code in that file.

src/Template/Authexs/index.php

You are successfully logged in. 
<?php 
   echo $this->Html->pnk( logout ,[
      "controller" => "Authexs","action" => "logout"
   ]); 
?>

Execute the above example by visiting the following URL.

http://localhost/cakephp4/auth

Output

As the authentication has been implemented, and once you try to visit the above URL, you will be redirected to the login page as shown below.

Authentication

After providing the correct credentials, you will be logged in and redirected to the screen as shown below.

Redirected

After cpcking on the logout pnk, you will be redirected to the login screen again.

Advertisements