控制器顧名思義控制應(yīng)用程序。它就像模型和視圖之間的橋梁。控制器處理請求數(shù)據(jù),確保調(diào)用正確的模型、響應(yīng)或視圖??刂破鞯念愔械姆椒ū环Q為行為 。每個控制器遵循以下命名規(guī)格??刂破黝惷菑?fù)數(shù)形式,駝峰結(jié)構(gòu),并以Controller結(jié)束-如:PostsController。
AppConttroller類是所有應(yīng)用程序控制器的父類。該類擴展至CakePHP的Controller類。 AppController在src/Controller/AppController.php中定義。該文件包含下面的代碼。
<?php namespace AppController; use CakeControllerController; use CakeEventEvent; class AppController extends Controller{ public function initialize(){ parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); } public function beforeRender(Event $event){ if (!array_key_exists('_serialize', $this->viewVars) && in_array($this->response->type(), ['application/json', application/xml'])) { $this->set('_serialize', true); } } }
AppController可以用來加載在你的應(yīng)用程序的每個控制器中會使用到的組件。在AppController中創(chuàng)建的屬性和方法可以在擴展至它的所有控制器中使用。initialize()方法將在構(gòu)造函數(shù)的末尾被調(diào)用來加載組件。
控制器類中的方法被稱為行為。行為負(fù)責(zé)給發(fā)出請求的瀏覽器/用戶發(fā)送適當(dāng)?shù)捻憫?yīng)。試圖通過行為來展現(xiàn),或者說通過控制器的方法來展現(xiàn)。
class RecipesController extends AppController{ public function view($id){ // Action logic goes here. } public function share($customerId, $recipeId){ // Action logic goes here. } public function search($query){ // Action logic goes here. } }
正如你可以在上面的例子中看到的,RecipesController具有3種行為- view,share和search 。
要重定向用戶到相同控制器的另一個行為,我們可以使用的setAction()方法。以下是用于setAction()方法的語法 -
CakeControllerController::setAction($action, $args...)
下面的代碼將用戶重定向到同一控制器的index行為。
$this->setAction('index');
以下示例顯示了上述方法的使用。
在以下項目中,修改如下config/routes.php文件。
config/routes.php文件
<?php use CakeCorePlugin; use CakeRoutingRouteBuilder; use CakeRoutingRouter; Router::defaultRouteClass('DashedRoute'); Router::scope('/', function (RouteBuilder $routes) { $routes->connect('/redirectcontroller',[' controller'=>'Redirects','action'=>'action1']); $routes->connect('/redirectcontroller2',[' controller'=>'Redirects','action'=>'action2']); $routes->fallbacks('DashedRoute'); }); Plugin::routes();
在src/Controller/下創(chuàng)建RedirectsController.php文件。復(fù)制以下代碼至其中。
src/Controller/RedirectsController.php
<?php namespace AppController; use AppControllerAppController; use CakeORMTableRegistry; use CakeDatasourceConnectionManager; class RedirectsController extends AppController{ public function action1(){ } public function action2(){ echo "redirecting from action2"; $this->setAction('action1'); } } ?>
在src/Template目錄下創(chuàng)建一個Redirects目錄,在此Redirects目錄下創(chuàng)建一個名為action1.ctp一個視圖(View)文件。復(fù)制以下代碼至其中。
src/Template/Redirects/action1.ctp
This is an example of how to redirect within controller.
通過訪問以下網(wǎng)址執(zhí)行上面的例子。
http://localhost:85/CakePHP/redirect-controller
執(zhí)行后,您會看到以下輸出。
現(xiàn)在,請訪問以下網(wǎng)址- http://localhost:85/CakePHP/redirect-controller2
上述網(wǎng)址會產(chǎn)生以下輸出。
在CakePHP中,一個模型可以使用loadModel()方法來加載。以下使用loadModel()方法的語法。
CakeControllerController::loadModel(string $modelClass, string $type)
上面的函數(shù)有兩個參數(shù)-
第一個參數(shù)是模型類的名字。
第二個參數(shù)是需要加載的庫的類型。
如果要在控制器中加載文章模型,那么可以通過給控制器的行為添加以下代碼進行加載。
$this->loadModel('Articles');
更多建議: