3

我正在尝试使用前缀路由设置 cakephp 4。在我的 home.php 中,我设置了以下代码(关注“Serving Ones”下的 html a href 标签):

<?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 } ?>

我的 routes.php,底部,在 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);
});

我在“服务”前缀下添加了该路由,该前缀专门提到了控制器和操作。

无论如何,当我运行代码时,我会立即收到找不到路由的错误:

匹配“array ('prefix' => 'serving', 'controller' => 'TrainingsUsers', 'action' => 'add', 'plugin' => NULL, '_ext' => NULL, )" 的路由可以找不到。

我环顾四周并试图查看文档(我对 cakephp 仍然很陌生),但无法找出这样做的原因。我的理解是回退默认路由应该捕获这个控制器/动作组合,但它没有。即使我自定义添加了路线(假设我做得正确),它仍然说找不到。

奇怪的是,当我从 Url::build 中的数组更改时:

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

在参数中使用字符串:

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

有用。在错误页面上的 Connected Routes 下,

我在这里错过了什么吗?

我刚想起来。我在 cakephp 4.0.1

我认为这是我的 error.log 中的相关信息。我的 debug.log 没有显示任何内容。

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

提前致谢

编辑: bin/cake 路线生成(仅左列):

+-------------------------------------+-------------------------------------------+----------------------------------------------------------------------------------+
| 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"} |
+-------------------------------------+-------------------------------------------+----------------------------------------------------------------------------------+
4

0 回答 0