以下文章提供了 CakePHP 授權的概述。 CakePHP 是一個開源工具,它以可插拔的方式提供 Auth 元件來執行我們的任務。 Auth元件用於提供身份驗證和授權物件。換句話說,我們可以說它是兩者的組合,用於根據我們的要求確定使用者的授權和身份驗證。身份驗證意味著確定使用者憑證並驗證這些憑證,例如使用者名稱和密碼。另一方面,授權意味著根據使用者憑證和使用者提供的其他資訊對使用者進行驗證。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
什麼是CakePHP授權?
正如您可能知道的,「最近」(不是最近)添加了兩個新模組來管理 CakePHP 應用程式中的身份驗證和授權的想法。從長遠來看,身份驗證和授權是在控制器層使用 AuthComponent 進行監督的。隨著任務的發展,這兩件事通常會變得錯綜複雜,使 AuthComponent 成為同時管理許多元素的令人困惑的類別。
這些新模組背後的第一個想法是重構 AuthComponent 並創建明確層來處理:
確認:你是誰?
批准:你說你被允許了嗎?
我們將利用特定模型研究本文中的授權想法:我們應該設想一些遊戲應用程序,用戶將在其中監督錦標賽。用戶希望舉辦新的錦標賽並透過具有眾多附屬關係的錦標賽會員資格加入錦標賽。除非歡迎客戶參加比賽,否則客戶不會參加錦標賽。錦標賽的玩家可以歡迎不同的使用者來玩。
如何檢查CakePHP授權?
現在讓我們看看如何檢查 CakePHP 授權,如下所示:
在我們各自的應用程式中實作授權中間件後,我們可以檢查授權。這是因為中間件包裝了每個請求的身份。
現在讓我們看看如何檢查單一資源的授權,如下所示:
他們可以幫助您真正查看單一資產的批准。通常這是一個 ORM 物質或應用領域物件。
您的政策給出了決定批准選擇的理由:
代碼:
// Fetch identity from each and every request $user = $this->request->getAttribute('identity'); // Checking authorization on $sample if ($user->can('delete', $sample)) { // Do delete operation }
現在讓我們來看看如何應用範圍條件,如下:
每當您需要對各種項目進行批准檢查(例如分頁查詢)時,您通常需要取得目前客戶接觸的記錄。這個模組將這個想法作為「範圍」來實現。
範圍方法可讓您「範圍」查詢或結果集並傳回刷新的綱要或問題物件:
代碼:
// Fetch the identity from each and every request $specified user = $this->request->getAttribute('identity'); $Sql_query = $specified fuser->ApplyScopeTo('index', $Sql_query);
授權組件可用於監管活動中以順利批准,從而提高失望的豁免率。
建立 CakePHP 授權
現在讓我們看看如何在 CakePHP 中建立授權,範例如下:
首先,我們需要了解需要考慮哪些參數,如下:
確認是區分合適客戶最常見的方式。 CakePHP 支援三種驗證。
- FormAuthenticate:它允許您確認給定結構化 POST 資訊的客戶端。通常,這是客戶端輸入資料的登入結構。這是預設的驗證策略。
- BasicAuthenticate:它允許您確認客戶端正在使用基本 HTTP 驗證。
- DigestAuthenticate:它允許您確認客戶端正在使用摘要 HTTP 驗證。
首先,我們需要設定routes.php文件,如下:
代碼:
<?php use Cake\Core\Plugin; use Cake\Routing\RouteBuilder; use Cake\Routing\Router; Router::defaultRouteClass('DRoute'); Router::scope('/', function (RouteBuilder $routes) { $routes->connect('/auth',['controller'=>'Auth','action'=>'index']); $routes->connect('/login',['controller'=>'Auth','action'=>'login']); $routes->connect('/logout',['controller'=>'Auth','action'=>'logout']); $routes->fallbacks('DRoute'); }); Plugin::routes();
之後,我們需要建立一個controller.php文件,並寫如下程式碼:
代碼:
<?php namespace App\Controller; use Cake\Controller\Controller; use Cake\Event\Event; use Cake\Controller\Component\AuthComponent; class DemoController extends Controller { public function initialize() { parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); $this->loadComponent('Auth', [ 'authenticate' => [ 'Form' => [ 'fields' => [ 'username' => 'userid', 'password' => 'userpass' ] ] ], 'loginAction' => [ 'controller' => 'Authexs', 'action' => 'login' ], 'loginRedirect' => [ 'controller' => 'Authexs', 'action' => 'index' ], 'logoutRedirect' => [ 'controller' => 'Authexs', 'action' => 'login' ] ]); } public function BFilter(Event $eventt) { $this->Auth->allow(['index','view']); $this->set('loggedIn', $this->Auth->specified user()); } }
現在建立 authcontrollr.php 檔案並編寫以下程式碼:
代碼:
<?php namespace App\Controller; use App\Controller\AppController; use Cake\ORM\TableRegistry; use Cake\Datasource\ConnectionManager; use Cake\Event\Eventt; use Cake\Auth\DefaultPasswordHasher; class AuthController extends AppController { var $component = array('Auth'); public function index(){ } public function login(){ if($this->request->is('post')) { $specified_user = $this->Auth->identify(); if($user){ $this->Auth->setUser($specified_user); return $this->redirect($this->Auth->redirectUrl()); } else $this->Flash->errormsg('Entered username and password is wrong'); } } public function logout(){ return $this->redirect($this->Auth->logout()); } }
最後,我們需要建立一個登入範本來查看結果,如下。
<?php echo $this->Form->create(); echo $this->Form->control('UserID'); echo $this->Form->control('Userpass'); echo $this->Form->button('Submit'); echo $this->Form->end(); ?>
說明:
這裡我們建立一個模板來查看結果。執行上述程式碼後,我們將得到以下畫面。
在這裡我們可以提供用於登入的使用者憑證。
我們必須建立另一個用於登出的 PHP 檔案並編寫以下程式碼。
代碼:
<?php echo $this->Html->link('logout',[ "controller" => "Auth","action" => "logout" ]); ?>
After executing the above code, we will get the following screen.
CakePHP Authorization Installing
Now let’s see how we can install authorization in CakePHP as follows:
First, we need to load the plugin by using the following statement as follows:
Code:
$this-> addPlugin('Authorization');
After that, we need to enable all authorization plugins by importing the following class as follows:
Code:
use Authorization\AuthorizationService; use Authorization\AuthorizationServiceInterface; use Authorization\AuthorizationServiceProviderInterface; use Authorization\Middleware\AuthorizationMiddleware; use Authorization\Policy\OrmResolver;
After creating a policy as per our requirement, we also need to fix add and edit action as per our requirement. The requirement mentioned above we can achieve through coding.
Conclusion
From the above article, we have taken in the essential idea of the CakePHP authorization and see the representation and example of the CakePHP authorization. Finally, we saw how and when we use the CakePHP authorization from this article.
以上是CakePHP授權的詳細內容。更多資訊請關注PHP中文網其他相關文章!

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP可以輕鬆創建互動網頁內容。 1)通過嵌入HTML動態生成內容,根據用戶輸入或數據庫數據實時展示。 2)處理表單提交並生成動態輸出,確保使用htmlspecialchars防XSS。 3)結合MySQL創建用戶註冊系統,使用password_hash和預處理語句增強安全性。掌握這些技巧將提升Web開發效率。

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP在現代Web開發中仍然重要,尤其在內容管理和電子商務平台。 1)PHP擁有豐富的生態系統和強大框架支持,如Laravel和Symfony。 2)性能優化可通過OPcache和Nginx實現。 3)PHP8.0引入JIT編譯器,提升性能。 4)雲原生應用通過Docker和Kubernetes部署,提高靈活性和可擴展性。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載
最受歡迎的的開源編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

禪工作室 13.0.1
強大的PHP整合開發環境