Home > Software engineering >  How to change route URL in Laravel 9 resource controller
How to change route URL in Laravel 9 resource controller

Time:02-25

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?

CodePudding user response:

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

  • Related