亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄
身份驗(yàn)證
FormCakePHP 服務(wù) 示例
輸出
首頁 后端開發(fā) php教程 CakePHP 服務(wù)

CakePHP 服務(wù)

Sep 10, 2024 pm 05:26 PM
php cakephp PHP framework

本章介紹有關(guān) CakePHP 中可用的身份驗(yàn)證過程的信息。

身份驗(yàn)證

身份驗(yàn)證是識(shí)別正確用戶的過程。 CakePHP 支持三種類型的身份驗(yàn)證。

  • FormAuthenticate - 它允許您根據(jù)表單 POST 數(shù)據(jù)對(duì)用戶進(jìn)行身份驗(yàn)證。通常,這是用戶輸入信息的登錄表單。這是默認(rèn)的身份驗(yàn)證方法。

  • BasicAuthenticate - 它允許您使用基本 HTTP 身份驗(yàn)證來驗(yàn)證用戶

  • DigestAuthenticate - 它允許您使用摘要式 HTTP 身份驗(yàn)證對(duì)用戶進(jìn)行身份驗(yàn)證。

FormCakePHP 服務(wù) 示例

在 config/routes.php 文件中進(jìn)行更改,如以下代碼所示。

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();

更改AppController.php文件的代碼,如以下程序所示。

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());
   }
}

src/Controller/AuthexsController.php 創(chuàng)建 AuthexsController.php 文件。 將以下代碼復(fù)制到控制器文件中。

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());
   } 
}
?>

src/Template 處創(chuàng)建一個(gè)目錄 Authexs 并在該目錄下創(chuàng)建一個(gè)名為 login.phpView 文件。將以下代碼復(fù)制到該文件中。

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

創(chuàng)建另一個(gè) View 文件,名為 logout.php。 將以下代碼復(fù)制到該文件中。

src/Template/Authexs/logout.php

You are successfully logged out.

創(chuàng)建另一個(gè) View 文件,名為 index.php。 將以下代碼復(fù)制到該文件中。

src/Template/Authexs/index.php

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

通過訪問以下 URL 來執(zhí)行上述示例。

http://localhost/cakephp4/auth

輸出

由于身份驗(yàn)證已完成,一旦您嘗試訪問上述網(wǎng)址,您將被重定向到如下所示的登錄頁面。

CakePHP 服務(wù)

提供正確的憑據(jù)后,您將登錄并重定向到如下所示的屏幕。

CakePHP 服務(wù)

點(diǎn)擊注銷鏈接后,您將再次重定向到登錄屏幕。

以上是CakePHP 服務(wù)的詳細(xì)內(nèi)容。更多信息請(qǐng)關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

如何用JavaScript判斷兩個(gè)數(shù)組是否相等? 如何用JavaScript判斷兩個(gè)數(shù)組是否相等? May 23, 2025 pm 10:51 PM

JavaScript中判斷兩個(gè)數(shù)組是否相等需要使用自定義函數(shù),因?yàn)闆]有內(nèi)置方法。1)基本實(shí)現(xiàn)通過比較長度和元素,但不能處理對(duì)象和數(shù)組。2)遞歸深度比較能處理嵌套結(jié)構(gòu),但需特別處理NaN。3)還需考慮函數(shù)、日期等特殊類型,需進(jìn)一步優(yōu)化和測試。

PHP中如何驗(yàn)證社保號(hào)字符串? PHP中如何驗(yàn)證社保號(hào)字符串? May 23, 2025 pm 08:21 PM

社保號(hào)驗(yàn)證在PHP中通過正則表達(dá)式和簡單邏輯實(shí)現(xiàn)。1)使用正則表達(dá)式清理輸入,去除非數(shù)字字符。2)檢查字符串長度是否為18位。3)計(jì)算并驗(yàn)證校驗(yàn)位,確保與輸入的最后一位匹配。

如何在閉包中正確處理this指向? 如何在閉包中正確處理this指向? May 21, 2025 pm 09:15 PM

在JavaScript閉包中正確處理this指向的方法有:1.使用箭頭函數(shù),2.使用bind方法,3.使用變量保存this。這些方法能確保內(nèi)部函數(shù)的this正確指向外部函數(shù)的上下文。

怎樣用JavaScript實(shí)現(xiàn)數(shù)據(jù)加密? 怎樣用JavaScript實(shí)現(xiàn)數(shù)據(jù)加密? May 23, 2025 pm 11:12 PM

使用JavaScript實(shí)現(xiàn)數(shù)據(jù)加密可以使用Crypto-JS庫。1.安裝并引入Crypto-JS庫。2.使用AES算法進(jìn)行加密和解密,確保使用相同的密鑰。3.注意密鑰的安全存儲(chǔ)和傳輸,推薦使用CBC模式和環(huán)境變量存儲(chǔ)密鑰。4.在高性能需求時(shí),考慮使用WebWorkers。5.處理非ASCII字符時(shí),需指定編碼方式。

PHP中如何定義構(gòu)造函數(shù)? PHP中如何定義構(gòu)造函數(shù)? May 23, 2025 pm 08:27 PM

在PHP中,構(gòu)造函數(shù)通過\_\_construct魔術(shù)方法定義。1)在類中定義\_\_construct方法,它會(huì)在對(duì)象實(shí)例化時(shí)自動(dòng)調(diào)用,用于初始化對(duì)象屬性。2)構(gòu)造函數(shù)可以接受任意數(shù)量的參數(shù),靈活初始化對(duì)象。3)在子類中定義構(gòu)造函數(shù)時(shí),需要調(diào)用parent::\_\_construct()確保父類構(gòu)造函數(shù)執(zhí)行。4)通過可選參數(shù)和條件判斷,構(gòu)造函數(shù)可以模擬重載效果。5)構(gòu)造函數(shù)應(yīng)簡潔,只做必要初始化,避免復(fù)雜邏輯或I/O操作。

在PhpStudy上部署Joomla網(wǎng)站的詳細(xì)步驟 在PhpStudy上部署Joomla網(wǎng)站的詳細(xì)步驟 May 16, 2025 pm 08:00 PM

在PhpStudy上部署Joomla網(wǎng)站的步驟包括:1)配置PhpStudy,確保Apache和MySQL服務(wù)運(yùn)行并檢查PHP版本兼容性;2)從Joomla官網(wǎng)下載并解壓到PhpStudy的網(wǎng)站根目錄,然后通過瀏覽器按照安裝向?qū)瓿砂惭b;3)進(jìn)行基本配置,如設(shè)置網(wǎng)站名稱和添加內(nèi)容。

PHP依賴注入:好處和例子 PHP依賴注入:好處和例子 May 17, 2025 am 12:14 AM

使用依賴注入(DI)在PHP中的好處包括:1.解耦,使代碼更模塊化;2.提高可測試性,易于使用Mocks或Stubs;3.增加靈活性,方便更換依賴;4.提升可重用性,類可在不同環(huán)境中使用。通過將依賴從外部傳遞給對(duì)象,DI使代碼更易維護(hù)和擴(kuò)展。

PHP電子郵件教程:發(fā)送電子郵件很容易 PHP電子郵件教程:發(fā)送電子郵件很容易 May 19, 2025 am 12:10 AM

sendingemailswithphpisstraightforwardusingthemail()functionormoreadvancedLibrariesLikeLikePhpMailer.1)usemail()forbasicemails,settrecipients,settrecipients,subjects,message,messages,andheaders.2)forhtmlemails,juspeStheadeStheadeSteStheadeSteStospeSpepeSpepeSpepeSpepeCifyHtmlConteN.3)

See all articles