問題描述
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');
這篇關于CakePHP 3:存在的路由缺少路由錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!