2
\$\begingroup\$

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:

<?php
$approutes =function (){

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

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

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

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

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

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

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

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

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

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

Route::group(['domain' => 'www.xxxx.com'],$siteroutes);
Route::group(['domain' => 'www.xxxx.dev'],$siteroutes);
\$\endgroup\$
3
\$\begingroup\$

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->mapWebRoutes($router);     //You can comment out this if you want... Completely your choice

  $this->mapSiteRoutes($router);

  $this->mapAppRoutes($router);

  //
}

Now add these functions at the end...

protected function mapSiteRoutes(Router $router)
{
  $router->group([
    'namespace' => $this->namespace . '\Site', 'middleware' => '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->group([
      'namespace' => $this->namespace . '\MyApp',
      'middleware' => ['web', 'auth'],
      'domain' => $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' => 'profile.update','uses' => '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 :)

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.