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

时间:2015-04-21 20:46:03

标签: php cakephp cakephp-3.0

CakePHP 3.0

我得到了#34;缺少路线"存在的路由的错误。

以下是我的路线:

#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();

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

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

enter image description here

请注意错误消息:

  

错误:路线匹配"阵列('动作' =>'添加','前缀' =>   ' admin','插件' => NULL,' controller' => '屏幕',' _ext' =>空值,   )"无法找到。

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

发生了什么事?

2 个答案:

答案 0 :(得分:13)

仔细查看堆栈跟踪,在调度过程中出现错误,您似乎认为,它正在您的视图模板中触发,您可能正在尝试创建指向该模板的链接add操作,反向路由找不到匹配的路由,因此出错。

解决方案应该是显而易见的,连接必要的路线,明确的是

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

抓住所有人

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

或者只是那些能够抓住一切的后备者

$routes->fallbacks('InflectedRoute');

答案 1 :(得分:0)

如果使用前缀admin,这项工作对我来说: -

Router::prefix('admin', function ($routes) {
    // Because you are in the admin scope,
    // you do not need to include the /admin prefix
    // or the admin route element.
    $routes->connect('/', ['controller' => 'Users', 'action' => 'index']);
    $routes->extensions(['json', 'xml']);
    // All routes here will be prefixed with `/admin`
    $routes->connect('/admin', ['controller' => 'Order', 'action' => 'index']);
    // And have the prefix => admin route element added.
    $routes->fallbacks(DashedRoute::class);
});