Laravel routes.php optimization My goals are:


Have 2 main domains .com and .dev for production and local installations
Have 2 main subdomains. One for the site and one of the application
Site must be public and application always to be secured.


My take on the above:

&lt;?php
$approutes =function (){

    Route::group(['middleware' =&gt; 'auth'], function(){
        Route::get('/', 'AdminController@index');

        Route::get('profile/{user}','ProfileController@index');

        Route::post('profile/{user}/update',array('as' =&gt; 'profile.update','uses' =&gt; 'ProfileController@update'));

        Route::resource('posts','PostsController');

        Route::resource('tags','TagsController');

        Route::resource('health','HealthController');

        Route::resource('health-categories','HealthCategoriesController');
    });

    Route::controllers([
        'auth' =&gt; 'Auth\AuthController',
        'password' =&gt; 'Auth\PasswordController'
    ]);
};

$siteroutes =function (){
    Route::get('/', 'SiteController@index');
};

Route::group(['domain' =&gt; 'my.xxxx.com'],$approutes);
Route::group(['domain' =&gt; 'my.xxxx.dev'],$approutes);

Route::group(['domain' =&gt; 'www.xxxx.com'],$siteroutes);
Route::group(['domain' =&gt; 'www.xxxx.dev'],$siteroutes);
First of all, I really don't like to mix up routes of two different domains/apps in the same routes.php or web.php. So, I prefer and would suggest you as well to follow the below approach, which seems much cleaner and much authentic. :)

Also, I believe www.abc.com or www.abc.dev are not required to be mentioned here... Laravel is intelligent enough to work this through for routes without subdomains... (especially when it is installed in the root directory of that domain).... If required... you can still follow the procedure I used in mapAppRoutes() below.

Go to App\Providers\RouteServiceProvider

Now in the map() function... Add these method calls... 

public function map(Router $router)
{
  $this-&gt;mapWebRoutes($router);     //You can comment out this if you want... Completely your choice

  $this-&gt;mapSiteRoutes($router);

  $this-&gt;mapAppRoutes($router);

  //
}


Now add these functions at the end... 

protected function mapSiteRoutes(Router $router)
{
  $router-&gt;group([
    'namespace' =&gt; $this-&gt;namespace . '\Site', 'middleware' =&gt; 'web',
  ], function ($router) {
    require app_path('Http/routes.site.php');
  });
}

protected function mapAppRoutes(Router $router)
{
  $domains = ['my.xxxxxx.com', 'my.xxxxxx.dev'];    //Sub-domains you support

  foreach($domains as $domain) {
    $router-&gt;group([
      'namespace' =&gt; $this-&gt;namespace . '\MyApp',
      'middleware' =&gt; ['web', 'auth'],
      'domain' =&gt; $domain
    ], function ($router) {
      require app_path('Http/routes.myapp.php');
    });
  }
}


Now create, app/Http/routes.site.php and app/Http/routes.myapp.php

In routes.site.php, add this... 

Route::get('/', 'SiteController@index');


In routes.myapp.php, add these....

Route::get('/', 'AdminController@index');

Route::get('profile/{user}','ProfileController@index');

Route::post('profile/{user}/update',array('as' =&gt; 'profile.update','uses' =&gt; 'ProfileController@update'));

Route::resource('posts','PostsController');

Route::resource('tags','TagsController');

Route::resource('health','HealthController');

Route::resource('health-categories','HealthCategoriesController');


You're good to go... :)

Please note that I have changed the namespace as well for the routes, so that you can make separate folders for your home site and subdomain sites and easily differentiate between the two... even by looking at the tree-view. This I believe is a much better way of structuring and dividing your code... Makes things simpler to read and navigate :)