精品熟女碰碰人人a久久,多姿,欧美欧美a v日韩中文字幕,日本福利片秋霞国产午夜,欧美成人禁片在线观看

CakePHP 控制器

cakephp 控制器

 

如名稱所示的控制器控制應(yīng)用程序。它就像模型和視圖之間的橋梁。控制器處理請(qǐng)求數(shù)據(jù),確保調(diào)用正確的模型并呈現(xiàn)正確的響應(yīng)或視圖。

控制器類中的方法稱為 動(dòng)作。每個(gè)控制器都遵循命名約定。 controller 類名稱采用復(fù)數(shù)形式,camel cased,并以 controller 結(jié)尾 — postscontroller

 

應(yīng)用控制器

appconttroller 類是所有應(yīng)用程序控制器的父類。這個(gè)類擴(kuò)展了 cakephp 的 controller 類。 appcontroller 定義在 src/controller/appcontroller.php。 該文件包含以下代碼。

declare(strict_types=1);
namespace app\controller;
use cake\controller\controller;
class appcontroller extends controller {
   public function initialize(): void {
      parent::initialize();
      $this--->loadcomponent('requesthandler');
      $this->loadcomponent('flash');
   }
}

appcontroller 可用于加載將在應(yīng)用程序的每個(gè)控制器中使用的組件。屬性和方法在 appcontroller 中創(chuàng)建的將在擴(kuò)展它的所有控制器中可用。 initialize() 方法將在控制器構(gòu)造函數(shù)的末尾調(diào)用以加載組件。

 

控制器動(dòng)作

控制器類中的方法稱為actions。這些操作負(fù)責(zé)為發(fā)出請(qǐng)求的瀏覽器/用戶發(fā)送適當(dāng)?shù)捻憫?yīng)。視圖以動(dòng)作名稱呈現(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 個(gè)操作- 查看、分享和 搜索

 

重定向

為了將用戶重定向到同一控制器的另一個(gè)動(dòng)作,我們可以使用 setaction() 方法。以下是 setaction() 方法的語法。

cake\controller\controller::setaction($action, $args...)

以下代碼會(huì)將用戶重定向到同一控制器的索引操作。

$this->setaction('index');

下面的例子展示了上述方法的用法。

 

示例

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

config/routes.php

use cake\http\middleware\csrfprotectionmiddleware;
use cake\routing\route\dashedroute;
use cake\routing\routebuilder;
$routes--->setrouteclass(dashedroute::class);
$routes->scope('/', function (routebuilder $builder) {
   // register scoped middleware for in scopes.
   $builder->registermiddleware('csrf', new csrfprotectionmiddleware([
      'httponly' => true,
   ]));
   $builder->applymiddleware('csrf'); 
   $builder->connect('/redirect-controller',['controller'=>'redirects','action'=>'action1']);
   $builder->connect('/redirect-controller2',['controller'=>'redirects','action'=>'action2']);
   $builder->fallbacks();
});

在 src/controller/redirectscontroller.php 創(chuàng)建一個(gè) redirectscontroller.php 文件。將以下代碼復(fù)制到控制器文件中。

src/controller/redirectscontroller.php

declare(strict_types=1);
namespace app\controller;
use cake\core\configure;
use cake\http\exception\forbiddenexception;
use cake\http\exception\notfoundexception;
use cake\http\response;
use cake\view\exception\missingtemplateexception;
class redirectscontroller extends appcontroller {
   public function action1() {
   }
   public function action2(){
      echo "redirecting from action2";
      $this--->setaction('action1');
   }
}

src/template 創(chuàng)建一個(gè) redirects 目錄,然后在該目錄下創(chuàng)建一個(gè)名為action1.php 的 view 文件。將以下代碼復(fù)制到該文件中。

src/template/redirects/action1.php

this is an example of how to redirect within controller.

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

http://localhost/cakephp4/redirect-controller

 

輸出

執(zhí)行后,您將收到以下輸出。

現(xiàn)在,訪問以下 url:http://localhost/cakephp4/redirect-controller2

上述 url 將為您提供以下輸出。

 

加載模型

在 cakephp 中,可以使用 loadmodel() 方法加載模型。以下是 loadmodel() 方法的語法:

cake\controller\controller::loadmodel(string $modelclass, string $type)

上述函數(shù)有兩個(gè)參數(shù)如下:

  • 第一個(gè)參數(shù)是模型類的名稱。
  • 第二個(gè)參數(shù)是要加載的存儲(chǔ)庫類型。
  •  

    示例

    如果你想在控制器中加載文章模型,那么可以通過在控制器的操作中編寫以下行來加載它。

    $this->loadmodel('articles');

    下一節(jié):cakephp 視圖

    cakephp 教程

    相關(guān)文章