如何更改 Laravel 9 资源控制器中的路由 URL

发布于 2025-01-10 08:18:16 字数 160 浏览 3 评论 0原文

我想知道是否有办法更改 Laravel 资源控制器的默认 URL。例如,对于基本的 CRUD 操作,对于创建,我们有一个 Laravel 默认创建的 /create 路由。可以将其更改为 /ask/new 或类似的内容吗?

I wondered if there is a way to change the default URLs of Laravel's resource controller. For example, for basic CRUD operation, for creating, we have a /create route made by default by Laravel. Can it be changed to /ask or /new or something like that?

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

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

发布评论

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

评论(1

等数载,海棠开 2025-01-17 08:18:16

您可以“本地化”创建的资源 URI,无需太多工作(添加到服务提供商的 boot 方法):

Route::resourceVerbs([
    'create' => 'new',
]);

这将包含对 Route::resource(... ) 使用“new”而不是“create”为 create 操作创建 URI。

如果您需要变得比类似的更复杂,您可以扩展 Illuminate\Routing\ResourceRegistrar 以您想要的任何方式覆盖它。您可以调用您的版本的实例或将其绑定到 Illuminate\Routing\ResourceRegistrar 容器,该容器会将其用于所有resource 调用。

Laravel 9.x - 文档 - 控制器 - 资源控制器 - 本地化资源URI

You can "localize" the resource URIs that are created without much work (Added to the boot method of a Service Provider):

Route::resourceVerbs([
    'create' => 'new',
]);

This would have all calls to Route::resource(...) create the URI with 'new' instead of 'create' for the create action.

If you need to get more complicated than something like that you could extend Illuminate\Routing\ResourceRegistrar to override it in any way you would like. You could call an instance of your version or bind it to the container for Illuminate\Routing\ResourceRegistrar which would use it for all resource calls.

Laravel 9.x - Docs - Controllers - Resource Controllers - Localizing Resource URIs

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