路由將您的URL映射至特定的控制器行為。在本節(jié)中,我們將看到如何實現(xiàn)路由,如何從URL中傳遞參數(shù)給控制器?行為,如何生成URL,以及如何重定向到一個特定的URL。通常情況下,路由由config/routes.php文件來執(zhí)行。可以以兩種方式來實現(xiàn) -
這里是一個呈現(xiàn)兩種類型路由的例子。
// Using the scoped route builder. Router::scope('/', function ($routes) { $routes->connect('/', ['controller' => 'Articles', 'action' => 'index']); }); // Using the static method. Router::connect('/', ['controller' => 'Articles', 'action' => 'index']);
這兩種方法都將執(zhí)行ArticlesController的index方法。兩種方法中域內(nèi)路由生成器提供更好的性能。
Router:: connect()方法用于連接路線。下面是該方法的語法 -
static CakeRoutingRouter::connect($route, $defaults =[], $options =[])
Router:: connect()方法有三個參數(shù)-
第一個參數(shù)是要匹配的URL模板。
第二個參數(shù)包含路由元素的默認值。
第三個參數(shù)包含路由的選項,一般包含正則表達式規(guī)則。
這里是一個路由的基本格式 -
$routes->connect( 'URL template', ['default' => 'defaultValue'], ['option' => 'matchingRegex'] );
修改如下圖config/routes.php文件。
config/routes.php文件
<?php use CakeCorePlugin; use CakeRoutingRouteBuilder; use CakeRoutingRouter; Router::defaultRouteClass('DashedRoute'); Router::scope('/', function (RouteBuilder $routes) { $routes->connect('/', ['controller' => 'Tests', 'action' => 'index']); $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']); $routes->fallbacks('DashedRoute'); }); Plugin::routes();
在src/Controller/中創(chuàng)建一個TestsController.php文件。復(fù)制以下代碼至其中。
src/Controller/TestsController.php
<?php namespace AppController; use AppControllerAppController; class TestsController extends AppController{ public function index(){ } } ?>
在src/Template目錄下創(chuàng)建一個文件夾Tests,并在Tests文件夾下創(chuàng)建一個名為index.ctp一個視圖文件 。復(fù)制以下代碼至此文件中。
src/Template/Tests/index.ctp
This is CakePHP tutorial and this is an example of connecting routes.
通過訪問以下網(wǎng)址執(zhí)行上面的例子。
http//localhost:85/CakePHP/
上面的URL將顯示如下頁面。
傳遞的參數(shù)是指在URL中傳遞的參數(shù),這些參數(shù)可以傳遞給控制器?行為。這些參數(shù)有3種方法傳遞給控制器。
下面的例子顯示我們?nèi)绾螌?shù)傳遞給控制器??的操作方法。
請訪問以下網(wǎng)址- http://localhost:85/CakePHP/Tests/value1/value2
這將匹配以下路由線路。
$routes->connect('tests/:arg1/:arg2', ['controller' => 'Tests', 'action' => 'index'],['pass' => ['arg1', 'arg2']]);
在這里,URL中的value1將被分配給參數(shù)1,value2將被分配給參數(shù)2。
一旦參數(shù)被傳遞給控制器??的操作方法,你可以用下面的語法獲取參數(shù)。
$args = $this->request->params[‘pass’]
傳遞給控制器操作方法的參數(shù)將被存儲在$args變量里。
參數(shù)也可以由以下語句傳遞到操作方法 -
$routes->connect('/', ['controller' => 'Tests', 'action' => 'index',5,6]);
上面的語句將傳遞兩個參數(shù)5和6 到TestController index()方法。
修改以下所示config / routes.php文件。
配置/ routes.php文件
<?php use CakeCorePlugin; use CakeRoutingRouteBuilder; use CakeRoutingRouter; Router::defaultRouteClass('DashedRoute'); Router::scope('/', function (RouteBuilder $routes) { $routes->connect('tests/:arg1/:arg2', ['controller' => 'Tests', 'action'=> 'index'],['pass' =>['arg1', 'arg2']]); $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']); $routes->fallbacks('DashedRoute'); }); Plugin::routes();
在src/Controller/下創(chuàng)建一個TestsController.php文件。復(fù)制以下代碼至此文件中。
src/Controller/TestsController.php
<?php namespace AppController; use AppControllerAppController; class TestsController extends AppController{ public function index($arg1,$arg2){ $this->set('argument1',$arg1); $this->set('argument2',$arg2); } } ?>
在src/Template目錄下創(chuàng)建一個文件夾Tests,并在Tests下創(chuàng)建一個名為index.ctp一個視圖文件。復(fù)制以下代碼至其中。
src/Template/Tests/index.ctp
This is CakePHP tutorial and this is an example of Passed arguments.<br /> Argument-1: <?=$argument1?><br /> Argument-2: <?=$argument2?><br />
通過訪問以下網(wǎng)址執(zhí)行上面的例子。
http://localhost:85/CakePHP/tests/Virat/Kunal
以上URL會顯示以下頁面。
更多建議: