cakephp3:带有前缀路由的命名空间错误

cakephp3: namespace error with prefix route

我正在尝试使用 cakephp 实现 REST api 3.

为了给出一个易于重现的问题示例,我从全新安装 cakephp 3.1.11 开始。 在 config/routes.php 中,我添加:

Router::prefix('/api', function ($routes) {
    $routes->fallbacks('DashedRoute');
});

如果我访问 http://mysite/prefixtest/api/Accounts 我得到(预期的)"Missing Controller error":

Error: AccountsController could not be found.

Error: Create the class AccountsController below in file: src/Controller//Api/AccountsController.php

<?php
namespace App\Controller\Api;

use App\Controller\AppController;

class AccountsController extends AppController
{

}

注意路径中的双斜杠和命名空间中的双反斜杠。

使用建议的代码在 src/Controller/Api/AccountsController.php(单个/在路径中)中创建 AccountsController 会导致错误:

Error: syntax error, unexpected '\' (T_NS_SEPARATOR), expecting identifier (T_STRING)
File src/Controller/Api/AccountsController.php
Line: 2 

不足为奇,因为在 php 中不允许命名空间中的双反斜杠。 删除额外的反斜杠让我回到 "Missing Controller error",可能是因为控制器不在 cakephp 期望的命名空间中。

我做错了什么?

与作用域或路由相反,前缀不应与前导斜杠连接。

另见 Cookbook > Routing > Prefix Routing