Yesterday, I’ve shared ideas on how to manage your Laravel’s route in Laravel: Managing Routes post.
Today, I want to share with you guys how to create easily the resourceful route, with or without prefix (yes, you can make route version too!).
First thing first, let’s create a new Laravel project and require the Artisan Extended package to your Laravel Project.
Once you’re done with installation of Artisan Extended package, you may run now in terminal the following commands:
php artisan make:route Post
This will create a new Routes directory in your app folder, and a file named Post.php(app/Routes/Post.php), and you will have AppRoutesPost::routes(); in your routes/web.php. You may run php artisan route:list to check the route created just now.
Now, let say you want to add prefix to your route:
php artisan make:route -p v1 Post
Then, you will have AppRoutesV1Post::routes(); in your routes/web.php
How about I want a prefix of V1, but I want to differentiate between admin and normal user? Here how you can do it:
php artisan make:route -p v1 Admin/User && php artisan make:route -p v1 User
Now you should have AppRoutesV1AdminUser::routes(); and AppRoutesV1User::routes(); in your routes/web.php.
That’s it. It’s easy now to manage your routes.
All your routes will be store in app/Routes directory from now on and you just need to update your route files manually in case you need some extra routes in the class.
Here are some other tricks you may use:
php artisan make:route -m web,auth -p v1 Post
This will create a route in app/Routes/V1/Post.php and having middlewares of web & auth in routes/web.php
php artisan make:route -m auth:api,jwt -p v1 -a Post
This will create a route in app/Routes/API/V1/Post.php and having middlewares of auth:api & jwt in routes/api.php
Please take note that, the make:route is intended for resourceful route.
More test & result: