For small-scale applications single route file is sufficient but large-scale applications its cumbersome to maintain and organize the routes.
The solution for this was creating multiple route files. For example, if you are developing web application and APIs in the same application than its elegant to create separate route files for each. Even you can create separate route files by module or feature.
Laravel 5 supports creating separate route files out of the box and multiple routes file can be registered in
RouteServiceProvider. In laravel 4 and below versions we include route files manually in the main route file.
Let me show you what I did. First I created a directory called Routes and moved the standard
routes.php file to there and changed that name to
web.routes.php and then I created an extra file called
Next, I changed the
RouteServiceProvider map method to contain the right calls to both route files and I have created the separate namespace for web and APIs.
NOTE: I have placed all of my web controllers under Web directory and api controller under Api directory. Following two lines indicate that, feel free to change this values as you wish.
That is it. In this tutorial we have created only two route files, in reality, you can add as many routes as you want, just follow the same logic. I hope this tutorial will help you to organize your routes and project effectively.
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.