Laravel Routes - Getting Organized Posted on December 06, 2015

Routes, how to getting organized

Once you start using Laravel and you notice all the giant benefits ;) After awhile you will start to notice that your routes.php file will start to become very unorganized, messy and just plain ugly.

Now you might be thinking that we can start to simplify our routes by using the resource command: Route::resource

As you can read from the documentation we can definitely use this. But if we are not using all the routes that get defined by the resource command or if we want more control over the routes we provide this method is not going to work.

The main routes file

As we all know the main routes file is called routes.php. This keeps track of all routes in our Laravel application. For this example I am going to take a look at a little side project I had created called MyBillr. I decided to split up my routes into containers such as "Bill", "Subscription", "Auth", etc.

This means that anything that has to do with the BillController or bills in generate, the route would be found in a file called Bill.php

Let's take a look at how we can accomplish this and start organizing things for the better.

Introducing the Routes folder

The first thing that I do is I create a new folder called Routes. This is located inside app\Http\, so you structure would look like this when you are finished

    ... etc

As you can see I have highlighted my change. Inside this folder is where I store all of my routes. In a small project I created a file called "Bill.php", let's take a look at it right now.


Route::group(['prefix' => 'bill'], function(){
    Route::get('/', array(
        'as'   => 'bill',
        'uses' => 'BillController@index')
    Route::get('/add/{company_id?}', array(
        'as'   => 'bill.add',
        'uses' => 'BillController@add')
    Route::post('/insert', array(
        'as'   => 'bill.insert',
        'uses' => 'BillController@insert')
    Route::get('/pay/{id}', array(
        'as'   => '',
        'uses' => 'BillController@pay')
    Route::post('/paid/{id}', array(
        'as'   => 'bill.paid',
        'uses' => 'BillController@paid')
    Route::get('/edit/{id}', array(
        'as'   => 'bill.edit',
        'uses' => 'BillController@edit')
    Route::post('/update/{id}', array(
        'as'   => 'bill.update',
        'uses' => 'BillController@update')
    Route::get('/delete/{id}', array(
        'as'   => 'bill.delete',
        'uses' => 'BillController@delete')

Here you can see anything that has to do with a bill, exists in this Routes file. Hopefully you can start to see how we can use this method to keep things clean. Again anything that has to do with "Authorization" will go into my "Auth.php" route file (sign up, forgot password, login, etc).

Now that we have our routes separated let's take a quick look at how we can use these different files. We could go and write our own ServiceProvider but that seems a little over kill for what we need. Let's just stick with the basics of PHP and use an old method that seems to be forgotten, the include statement :)


Route::get('/', [
    'as'   => 'home',
    'uses' => 'HomeController@index'


There you go. That is my entire routes.php file, cleanly organized and very maintainable.

Closing Statement

Again this may not be the best way to organize routes, but this is the method that I have found to work the best. Always take what you read on the internet with a grain of salt, and you as the developer make the decision on what works best for you.

Share this post