CakePHP 3.9 路由前缀

CakePHP 3.9 Routing prefixes

我的 / 范围内有以下路径:

 $routes->connect('/api/:controller/:action', ['prefix'=>'api'], ['routeClass' => 'DashedRoute']);

在我的 src/Controller/UsersController.php 我有 api_index()

但是当我转到 url api/Users/index 时它说 找不到控制器,因为要求我在名为 [的子文件夹中添加另一个 UsersController =30=] 在 Controller 文件夹上。

直到 Cakephp 2.x 使用此行为对我来说效果很好,我怎样才能在 CakePHP 3.x 上实现与在 Cakephp 上相同的行为 2.x ?

非常感谢!

来自cakephp书

前缀路由 静态 Cake\Routing\Router::prefix($name, $callback) 许多应用程序需要一个管理部分,特权用户可以在其中进行更改。这通常通过特殊的 URL 来完成,例如 /admin/users/edit/5。在 CakePHP 中,可以使用前缀范围方法启用前缀路由:

use Cake\Routing\Route\DashedRoute;

Router::prefix('admin', function (RouteBuilder $routes) {
    // All routes here will be prefixed with `/admin`
    // And have the prefix => admin route element added.
    $routes->fallbacks(DashedRoute::class);
})

;

前缀映射到应用程序控制器命名空间中的 sub-namespaces。通过将前缀作为单独的控制器,您可以创建更小、更简单的控制器。前缀控制器和 non-prefixed 控制器共有的行为可以使用继承、组件或特征进行封装。使用我们的用户示例,访问 URL /admin/users/edit/5 将调用我们传递的 src/Controller/Admin/UsersController.php 的 edit() 方法5 作为第一个参数。使用的视图文件是 src/Template/Admin/Users/edit.ctp

您可以使用以下路径将 URL /admin 映射到页面控制器的 index() 操作:

Router::prefix('admin', function (RouteBuilder $routes) {
    // Because you are in the admin scope,
    // you do not need to include the /admin prefix
    // or the admin route element.
    $routes->connect('/', ['controller' => 'Pages', 'action' => 'index']);
});

https://book.cakephp.org/3/en/development/routing.html#prefix-routing