Home > Backend Development > PHP Tutorial > CakePHP Services

CakePHP Services

WBOY
Release: 2024-09-10 17:26:04
Original
877 people have browsed it

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

CakePHP Services

CakePHP Services 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 FormCakePHP Services

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

config/routes.php

<?php use Cake\Core\Plugin;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
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();
Copy after login

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

src/Controller/AppController.php

<?php namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Event\Event;
use Cake\Controller\Component\AuthComponent;
class AppController extends Controller {
   public function initialize() {
      parent::initialize();
      $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'
      ]
   ]);
}
public function beforeFilter(Event $event) {
      $this->Auth->allow(['index','view']);
      $this->set('loggedIn', $this->Auth->user());
   }
}
Copy after login

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

src/Controller/AuthexsController.php

<?php namespace App\Controller;
use App\Controller\AppController;
use Cake\ORM\TableRegistry;
use Cake\Datasource\ConnectionManager;
use Cake\Event\Event;
use Cake\Auth\DefaultPasswordHasher;
class AuthexsController extends AppController {
   var $components = array('Auth');
   public function index(){
   }
   public 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.');
      }
   }
   public function logout(){
      return $this->redirect($this->Auth->logout());
   } 
}
?>
Copy after login

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();
?>
Copy after login

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

src/Template/Authexs/logout.php

You are successfully logged out.
Copy after login

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->link('logout',[
      "controller" => "Authexs","action" => "logout"
   ]); 
?>
Copy after login

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.

CakePHP Services

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

CakePHP Services

After clicking on the logout link, you will be redirected to the login screen again.

The above is the detailed content of CakePHP Services. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template