CakePHP 3:存在的路由缺少路由错误

2021-12-21 00:00:00 php cakephp-3.0 cakephp

CakePHP 3.0

CakePHP 3.0

对于存在的路线,我收到了缺少路线"的错误消息.

I'm getting a "Missing Route" error for a route that exists.

这是我的路线:

#my admin routes...
Router::prefix('admin', function($routes) {
    $routes->connect('/', ['controller'=>'Screens', 'action'=>'index']);
    $routes->connect('/screens', ['controller'=>'Screens', 'action'=>'index']);
    $routes->connect('/screens/index', ['controller'=>'Screens', 'action'=>'index']);
    //$routes->fallbacks('InflectedRoute');
});

Router::scope('/', function ($routes) {

    $routes->connect('/login', ['controller' => 'Pages', 'action' => 'display', 'login']);    
    $routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);

    $routes->fallbacks('InflectedRoute');
});

Plugin::routes();

基本上我只是将顶部(用于管理路由)添加到开箱即用的默认路由中.

Basically I just added the top section (for admin routing) to the default routes that come out of the box.

当我访问 /admin/screens/index 时,我看到以下错误:

When I visit /admin/screens/index I see the following error:

注意错误信息说:

错误:匹配array ('action' => 'add', 'prefix' =>'admin', 'plugin' => NULL, 'controller' => 'Screens', '_ext' => NULL,)"无法找到.

Error: A route matching "array ( 'action' => 'add', 'prefix' => 'admin', 'plugin' => NULL, 'controller' => 'Screens', '_ext' => NULL, )" could not be found.

...这很奇怪,因为我没有尝试访问 add 操作.下面打印的参数看起来是正确的.

...which is strange because I am not trying to access the add action. The params printed below look correct.

这是怎么回事?

推荐答案

仔细看一下stacktrace,错误dosn't occurring in the dispatching process,你似乎认为,它是在你的视图模板中触发的,您可能正在尝试创建指向 add 操作的链接,并且反向路由找不到匹配的路由,因此出现错误.

Take a closer look at the stacktrace, the error dosn't occour in the dispatching process, which you seem to think, it is being triggered in your view template, where you are probably trying to create a link to the add action, and reverse-routing cannot find a matching route, hence the error.

解决方案应该是显而易见的,连接必要的路由,像

The solution should be obvious, connect the necessary routes, being it explicit ones like

$routes->connect('/screens/add', ['controller' => 'Screens', 'action' => 'add']);

包罗万象

$routes->connect('/screens/:action', ['controller' => 'Screens']);

或者只是捕获一切的后备

or simply the fallback ones that catch everything

$routes->fallbacks('InflectedRoute');

相关文章