CAKEPHP 3.9路由前缀
我的 / 范围内有以下路径:
$routes->connect('/api/:controller/:action', ['prefix'=>'api'], ['routeClass' => 'DashedRoute']);
在我的 src/Controller/UsersController.php 上,我有 api_index()
但是当我转到 url api/Users/index 时> 它说未找到控制器,因为要求我在 Controller 文件夹上名为 Api 的子文件夹中添加另一个 UsersController 。
直到 Cakephp 2.x 使用此行为对我来说效果很好,我如何才能在 CakePHP 3.x 上实现与 Cakephp 2.x 上相同的行为?
非常感谢 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cakephp book
前缀路由
静态蛋糕\路由\路由器::前缀($ name,$ callback)
许多应用程序需要一个管理部分,其中特权用户可以进行更改。这通常是通过特殊URL来完成的,例如
/admin/users/edit/5
。在CakePHP中,可以使用前缀示波器方法启用前缀路由:;
前缀映射到应用程序控制器名称空间中的子名称空间。通过将前缀作为单独的控制器,您可以创建较小,更简单的控制器。可以使用继承,组件或性状封装前缀和未装置控制器的共同和未装置控制器的行为。使用我们的用户示例,访问URL/admin/users/edit/5将调用我们的src/controller/ admin /usererscontroller.php的edit()方法,将5传给第一个参数。使用的视图文件将是src
/template/admin/users/edit.ctp
您可以使用以下路由将url/admin映射到页面控制器的index()操作:
From cakephp book
Prefix Routing
static Cake\Routing\Router::prefix($name, $callback)
Many applications require an administration section where privileged users can make changes. This is often done through a special URL such as
/admin/users/edit/5
. In CakePHP, prefix routing can be enabled by using the prefix scope method:;
Prefixes are mapped to sub-namespaces in your application’s Controller namespace. By having prefixes as separate controllers you can create smaller and simpler controllers. Behavior that is common to the prefixed and non-prefixed controllers can be encapsulated using inheritance, Components, or traits. Using our users example, accessing the URL /admin/users/edit/5 would call the edit() method of our src/Controller/Admin/UsersController.php passing 5 as the first parameter. The view file used would be src
/Template/Admin/Users/edit.ctp
You can map the URL /admin to your index() action of pages controller using following route:
https://book.cakephp.org/3/en/development/routing.html#prefix-routing