如何更改 Laravel 9 资源控制器中的路由 URL
我想知道是否有办法更改 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以“本地化”创建的资源 URI,无需太多工作(添加到服务提供商的
boot
方法):这将包含对
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):This would have all calls to
Route::resource(...)
create the URI with 'new' instead of 'create' for thecreate
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 forIlluminate\Routing\ResourceRegistrar
which would use it for allresource
calls.Laravel 9.x - Docs - Controllers - Resource Controllers - Localizing Resource URIs