CAKEPHP 3.9路由前缀

发布于 2025-01-18 08:39:48 字数 437 浏览 4 评论 0 原文

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

 $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 上相同的行为?

非常感谢 !

I have the following path inside my / Scope:

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

And on my src/Controller/UsersController.php i have api_index()

But when i go to the url api/Users/index it says Controller not found, because is asking me to add another UsersController inside a subfolder named Api on Controller folder.

Untill Cakephp 2.x using this behavior worked fine for me, how can i achieve the same behavior on CakePHP 3.x like it was on Cakephp 2.x ?

Thank you very much !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

总攻大人 2025-01-25 08:39:48

cakephp book

前缀路由
静态蛋糕\路由\路由器::前缀($ 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);
})

;

前缀映射到应用程序控制器名称空间中的子名称空间。通过将前缀作为单独的控制器,您可以创建较小,更简单的控制器。可以使用继承,组件或性状封装前缀和未装置控制器的共同和未装置控制器的行为。使用我们的用户示例,访问URL/admin/users/edit/5将调用我们的src/controller/ admin /usererscontroller.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']);
});

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:

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);
})

;

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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文