Jose Jimenez
Jose Jimenez
Software Architect & Developer
> >

Laravel environment domain routing

Published in Laravel on Aug 1, 2014

When working on a Laravel project you will typically have different environments, this tutorial will show you how to manage your domains so that they work properly in any of them.

##Extend Laravel Domain Example

To start I will use the example given in the Laravel documentation example, however we will be extending it. Let's copy the code from the documentation into app/routes.php. The only difference is that we will replace '{account}.myapp.com' with Config::get('route.account_domain'), this will allow us to use the config class to load variables based on the environment.

1Route::group(array('domain' => Config::get('route.account_domain')), function()
2{
3 Route::get('user/{id}', function($account, $id)
4 {
5 //
6 });
7});

###Production Environment

Let's start by creating a new file app/config/route.php, this will hold all of our routing domains, and allow us to overwrite it per domain. In this file add:

1return array(
2 'account_domain' => '{account}.myapp.com',
3);

This means that anytime the following domain gets hit, the route will match the domain and execute.

1http://jose.myapp.com

####Local Environment

This next example will allow us to overwrite the production environment variable in the event that we are loading the site locally, create another file app/config/local/route.php

1return array(
2 'account_domain' => '{account}.myapp.local',
3);

You will be able to hit the following domain, the route will match and just like in production it will execute.

1http://jose.myapp.local

Hope that you guys found this tutorial helpful, and like everything else please let me know if you guys have any suggestions.