【问题标题】:CakePHP 3: Missing route error for route that existsCakePHP 3:存在的路由缺少路由错误
【发布时间】:2015-06-29 05:44:33
【问题描述】:

CakePHP 3.0

对于存在的路线,我收到“缺少路线”错误。

这是我的路线:

#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 时,我看到以下错误:

请注意错误消息:

错误:路由匹配“数组('action' => 'add', 'prefix' => '管理员','插件' => NULL,'控制器' => '屏幕','_ext' => NULL, )" 找不到。

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

发生了什么事?

【问题讨论】:

  • 呃蛋糕。使用 2.0 时,我遇到了缓存模型的问题。我发现将 debug 设置为 2 可以解决问题。我假设你已经清除了缓存等
  • 调试配置似乎发生了一些变化。它现在是一个布尔值,我已将其设置为 TRUE。为了安全起见,我还删除了 tmp/ 中的所有缓存文件,它仍在执行此操作。
  • 那会耗尽我的首选。看起来 ndm 有更多有用的见解

标签: php cakephp cakephp-3.0


【解决方案1】:

仔细查看堆栈跟踪,在调度过程中不会发生错误,您似乎认为,它是在您的视图模板中触发的,您可能正在尝试创建指向 @987654321 的链接@action,反向路由找不到匹配的路由,所以报错。

解决方案应该很明显,连接必要的路线,就像明确的路线一样

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

包罗万象

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

或者只是捕获所有内容的后备

$routes->fallbacks('InflectedRoute');

【讨论】:

  • 你又成功了!!
  • 你这个人@ndm!
【解决方案2】:

如果使用前缀 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);
}); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    相关资源
    最近更新 更多