4

I'm trying to set up cakephp 4 with prefix routing. In my home.php, I have the following code set up (focusing on the html a href tag under "Serving Ones"):

<?php if ($prefix == 'serving' || $prefix == 'admin') {?>
<div class="row">
    <div class="column">
        <h4>Serving Ones</h4>
        <ul>
            <li class="bullet success">
                <a href=<?php echo $this->Url->build(['prefix' => 'serving', 'controller' => 'TrainingsUsers', 'action' => 'add']); ?>>Process Registrations</a>
                <?php /**<a href=<?php echo $this->Url->build('/serving/TrainingsUsers/process'); ?>>Process Registrations</a>**/?>
            </li>
        </ul>
    </div>
    <div class="column">
        <h4>Administration</h4>
        <ul>
            <li class="bullet success">
                Add/Edit Current Training
            </li>
        </ul>
    </div>
</div>
<hr>
<?php } ?>

My routes.php, the bottom part, after everything else auto-generated by cakephp.

$routes->prefix('admin' , function (RouteBuilder $admin_routes) {
    //$admin_routes->connect('/',['controller' => 'Pages', 'action' => 'display', 'home']);
    //$admin_routes->connect('/users/:action',['controller' => 'Users', 'action' => 'index']);

    //$admin_routes->connect('/TrainingsUsers/add',['controller' => 'TrainingsUsers', 'action' => 'add']);

    //$admin_routes->fallbacks(DashedRoute::class);
    $admin_routes->fallbacks(InflectedRoute::class);
});

$routes->prefix('serving' , function (RouteBuilder $serving_routes) {

    //$serving_routes->connect(':controller/:action');

    $serving_routes->connect('/TrainingsUsers/add',['controller' => 'TrainingsUsers', 'action' => 'add']);

    $serving_routes->fallbacks(DashedRoute::class);
    //$serving_routes->fallbacks(InflectedRoute::class);
});

I had that route added under the "serving" prefix that specifically mentions the controller and action.

Anyways, when I run the code, I immediately get the route not found error:

A route matching "array ( 'prefix' => 'serving', 'controller' => 'TrainingsUsers', 'action' => 'add', 'plugin' => NULL, '_ext' => NULL, )" could not be found.

I've looked around and tried to look at the docs (I'm still quite unfamiliar with cakephp) but could not find out why this is doing this. My understanding is that the fallback default route should catch this controller/action combo, but it doesn't. Even when I custom add the route (assuming I did it correctly), it's still saying not found.

The weird thing is, when I change from the array in the Url::build:

<a href=<?php echo $this->Url->build(['prefix' => 'serving', 'controller' => 'TrainingsUsers', 'action' => 'add']); ?>>Process Registrations</a>

to using the string in the arguments:

<a href=<?php echo $this->Url->build('/serving/TrainingsUsers/add'); ?>>Process Registrations</a>

it works. on the error page, under Connected Routes,

Am I missing something here?

I just remembered. I'm on cakephp 4.0.1

I think this is the relevant information from my error.log. My debug.log doesn't show anything.

2020-01-07 12:58:55 Error: [Cake\Routing\Exception\MissingRouteException] A route matching "array (
  'prefix' => 'serving',
  'controller' => 'TrainingsUsers',
  'action' => 'add',
  'plugin' => NULL,
  '_ext' => NULL,
)" could not be found. in C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Routing\RouteCollection.php on line 341
Exception Attributes: array (
  'url' => 'array (
  \'prefix\' => \'serving\',
  \'controller\' => \'TrainingsUsers\',
  \'action\' => \'add\',
  \'plugin\' => NULL,
  \'_ext\' => NULL,
)',
  'context' => 
  array (
    '_scheme' => 'http',
    '_host' => 'localhost',
    '_port' => NULL,
    '_base' => '/cake4/psrp',
    'params' => 
    array (
      'pass' => 
      array (
        0 => 'home',
      ),
      'controller' => 'Pages',
      'action' => 'display',
      'plugin' => NULL,
      '_matchedRoute' => '/',
      '_ext' => NULL,
    ),
  ),
)
Stack Trace:
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Routing\Router.php:494
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\View\Helper\UrlHelper.php:51
- C:\xampp\htdocs\cake4\psrp\templates\Pages\home.php:75
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\View\View.php:1164
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\View\View.php:1125
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\View\View.php:750
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Controller\Controller.php:691
- C:\xampp\htdocs\cake4\psrp\src\Controller\PagesController.php:97
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Controller\Controller.php:524
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Controller\ControllerFactory.php:79
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\BaseApplication.php:229
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Runner.php:77
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\authentication\src\Middleware\AuthenticationMiddleware.php:122
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Runner.php:77
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Middleware\CsrfProtectionMiddleware.php:132
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Runner.php:58
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php:162
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php:68
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php:118
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\debug_kit\src\Middleware\DebugKitMiddleware.php:60
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Runner.php:73
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Runner.php:58
- C:\xampp\htdocs\cake4\psrp\vendor\cakephp\cakephp\src\Http\Server.php:90
- C:\xampp\htdocs\cake4\psrp\webroot\index.php:40

Request URL: /
Referer URL: http://localhost/cake4/psrp/login?redirect=%2Fcake4%2Fpsrp%2F

Thanks in advance

EDIT: bin/cake routes generates (just the left column):

+-------------------------------------+-------------------------------------------+----------------------------------------------------------------------------------+
| Route name                          | URI template                              | Defaults                                                                         |
+-------------------------------------+-------------------------------------------+----------------------------------------------------------------------------------+
| pages:display                       | /                                         | {"0":"home","action":"display","controller":"Pages","plugin":null}               |
| pages:display                       | /pages/*                                  | {"action":"display","controller":"Pages","plugin":null}                          |
| login                               | /login                                    | {"action":"login","controller":"Users","plugin":null}                            |
| logout                              | /logout                                   | {"action":"logout","controller":"Users","plugin":null}                           |
| _controller:index                   | /{controller}                             | {"action":"index","plugin":null}                                                 |
| _controller:_action                 | /{controller}/{action}/*                  | {"action":"index","plugin":null}                                                 |
| admin:_controller:index             | /admin/{controller}                       | {"action":"index","plugin":null,"prefix":"Admin"}                                |
| admin:_controller:_action           | /admin/{controller}/{action}/*            | {"action":"index","plugin":null,"prefix":"Admin"}                                |
| serving:trainingsusers:_action      | /serving/TrainingsUsers/:action/*         | {"action":"add","controller":"TrainingsUsers","plugin":null,"prefix":"Serving"}  |
| serving:_controller:index           | /serving/{controller}                     | {"action":"index","plugin":null,"prefix":"Serving"}                              |
| serving:_controller:_action         | /serving/{controller}/{action}/*          | {"action":"index","plugin":null,"prefix":"Serving"}                              |
| debugkit.toolbar:clearcache         | /debug-kit/toolbar/clear-cache            | {"action":"clearCache","controller":"Toolbar","plugin":"DebugKit"}               |
| debugkit.requests:view              | /debug-kit/toolbar/*                      | {"action":"view","controller":"Requests","plugin":"DebugKit"}                    |
| debugkit.panels:view                | /debug-kit/panels/view/*                  | {"action":"view","controller":"Panels","plugin":"DebugKit"}                      |
| debugkit.panels:index               | /debug-kit/panels/*                       | {"action":"index","controller":"Panels","plugin":"DebugKit"}                     |
| debugkit.composer:checkdependencies | /debug-kit/composer/check-dependencies    | {"action":"checkDependencies","controller":"Composer","plugin":"DebugKit"}       |
| debugkit.mailpreview:index          | /debug-kit/mail-preview                   | {"action":"index","controller":"MailPreview","plugin":"DebugKit"}                |
| debugkit.mailpreview:email          | /debug-kit/mail-preview/preview           | {"action":"email","controller":"MailPreview","plugin":"DebugKit"}                |
| debugkit.mailpreview:email          | /debug-kit/mail-preview/preview/*         | {"action":"email","controller":"MailPreview","plugin":"DebugKit"}                |
| debugkit.mailpreview:sent           | /debug-kit/mail-preview/sent/{panel}/{id} | {"action":"sent","controller":"MailPreview","plugin":"DebugKit"}                 |
| debugkit.dashboard:index            | /debug-kit                                | {"_method":"GET","action":"index","controller":"Dashboard","plugin":"DebugKit"}  |
| debugkit.dashboard:index            | /debug-kit/dashboard                      | {"_method":"GET","action":"index","controller":"Dashboard","plugin":"DebugKit"}  |
| debugkit.dashboard:reset            | /debug-kit/dashboard/reset                | {"_method":"POST","action":"reset","controller":"Dashboard","plugin":"DebugKit"} |
+-------------------------------------+-------------------------------------------+----------------------------------------------------------------------------------+
David Wang
  • 41
  • 3
  • 1
    Whenever you have routing problems, start with checking what routes exactly are connected in which order, using for example [**the routes command**](https://book.cakephp.org/4/en/console-commands/routes.html): `bin/cake routes` – ndm Jan 07 '20 at 23:42
  • Edited my original question. When I got the error page, I also looked at the routes displayed on that page and it seems like the route is there and should be captured by the fallback. – David Wang Jan 08 '20 at 01:34
  • 1
    Check the defaults, the prefix name is `Serving`, with an uppercase `S`. Looks like that was changed in 4.x, prefixes are now camel cased: **https://book.cakephp.org/4/en/appendices/4-0-migration-guide.html#router** – ndm Jan 08 '20 at 02:10
  • Oh my goodness! Yea that's right. I need to change the prefix in the array to camel cased. But in the URL and elsewhere it remains lowercase but it works. This is so weird. Thanks a lot for the help! – David Wang Jan 08 '20 at 06:27

0 Answers0