使用前缀路由时url中的CakePHP3.x控制器名称
我试图在CakePHP3中使用前缀路由。我在/config/routes.php中添加了以下行使用前缀路由时url中的CakePHP3.x控制器名称,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,我试图在CakePHP3中使用前缀路由。我在/config/routes.php中添加了以下行 Router::prefix("admin", function($routes) { // All routes here will be prefixed with ‘/admin‘ // And have the prefix => admin route element added. $routes->connect("/",["controller"=&g
Router::prefix("admin", function($routes) {
// All routes here will be prefixed with ‘/admin‘
// And have the prefix => admin route element added.
$routes->connect("/",["controller"=>"Tops","action"=>"index"]);
$routes->connect("/:controller", ["action" => "index"]);
$routes->connect("/:controller/:action/*");
});
之后,我创建了/src/Controller/Admin/QuestionsController.php,如下所示
<?php
namespace App\Controller\Admin;
use App\Controller\AppController;
class QuestionsController extends AppController {
public function index() {
//some code here
}
}
?>
最后,我尝试访问localhost/app\u name/admin/questions/index
,但出现了一个错误,错误是,错误:找不到questionsController
。但是,当我将控制器名称的第一个字母大写时(即localhost/app_name/admin/Questions/index),它工作正常。我觉得这很奇怪,因为没有前缀,我可以使用第一个字符不大写的控制器名称。
这是某种错误吗?在Cake 3.x中,路由在默认情况下不再弯曲,相反,您必须显式使用
弯曲路由
路由类,例如可以在默认的路由.php
应用程序配置中看到:
Router::scope('/', function($routes) {
// ...
/**
* Connect a route for the index action of any controller.
* And a more general catch all route for any action.
*
* The `fallbacks` method is a shortcut for
* `$routes->connect('/:controller', ['action' => 'index'], ['routeClass' => 'InflectedRoute']);`
* `$routes->connect('/:controller/:action/*', [], ['routeClass' => 'InflectedRoute']);`
*
* You can remove these routes once you've connected the
* routes you want in your application.
*/
$routes->fallbacks();
});
自定义路由不指定特定的路由类,因此使用默认的路由
类,而回退路由使用屈折路由,这就是它不使用前缀的原因
因此,要么在URL中使用大写的控制器名称,要么使用像infictedroute
这样的路由类来正确转换它们:
Router::prefix('admin', function($routes) {
// All routes here will be prefixed with ‘/admin‘
// And have the prefix => admin route element added.
$routes->connect(
'/',
['controller' => 'Tops', 'action' => 'index']
);
$routes->connect(
'/:controller',
['action' => 'index'],
['routeClass' => 'InflectedRoute']
);
$routes->connect(
'/:controller/:action/*',
[],
['routeClass' => 'InflectedRoute']
);
});
另请参见再次感谢:)从2.x开始,很多事情都发生了变化。我需要花更多的时间来读这本书。