在 Cake 3.x 中,默认情况下路由不再变形,相反,您必须显式地使用InflectedRoute
路由类,例如默认情况下可以看到的蜜蜂routes.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();
});
您自定义的路由没有指定特定的路由类别,因此默认Route
正在使用类,而后备路由则使用变形路由,这就是它在没有前缀的情况下工作的原因。
因此,要么在 URL 中使用大写的控制器名称,要么使用类似的路由类InflectedRoute
正确地转换它们:
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']
);
});
也可以看看http://book.cakephp.org/3.0/en/development/routing.html#route-elements http://book.cakephp.org/3.0/en/development/routing.html#route-elements