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}' with Config::get('route.account_domain'), this will allow us to use the config class to load variables based on the environment.

Route::group(array('domain' => Config::get('route.account_domain')), function()
    Route::get('user/{id}', function($account, $id)

###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:

return array(
	'account_domain' => '{account}',

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

####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

return array(
	'account_domain' => '{account}.myapp.local',

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


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