Multiple Route files in Laravel 5

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.

Multiple Route files in Laravel 5

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 api.routes.php

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.

0 0 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Norges
Norges
1 year ago

Hi. I’m working with multiple modules in Laravel 5.6, and I get multiple file with RouteService Provider.
All be fine until I need generate form action url.

Form::model(null, [‘action’ => ‘[email protected]’, ‘id’ => ‘form_bus’, ‘class’ => ‘needs-validation’, ‘novalidate’])

Url generated for action is not fine because it contains a last rootnamespace of route service provide from container route service provider.

Form is a CollectiveHtmlFormBuilder from laravelcollective/html package.

Can you help me?

DMCA.com Protection Status
1
0
Would love your thoughts, please comment.x
()
x