Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

CakePHP 3.0

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

Here are my routes:

#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.

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

enter image description here

Notice the error message says:

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

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

What is going on?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
548 views
Welcome To Ask or Share your Answers For Others

1 Answer

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']);

catch-all ones

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

or simply the fallback ones that catch everything

$routes->fallbacks('InflectedRoute');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...