Javier Enríquez
Javier Enríquez

Reputation: 630

Laravel. Make redirects between subdomains

I'm developing an app with an admin subdomain, so I have admin.example.com and www.example.com. My problem is: whenever an user tries to enter admin.example.com he should be redirected to a login page (if not logged in), which is in www.example.com/login

How can I make a redirect from admin.example.com to www.example.com/login ?? I've tried this in my app/routes.php:

Route::group(['domain' => 'admin.example.com'], function(){
  Route::get('/', 'AdminHomeController@getWelcome');
  Route::any("{all}", function(){App::abort(404);})->where('all', '.*');
  // I used this line to block access routes in admin.example.com not defined here
});

Route::group(['domain' => 'www.example.com'], function()
{
..... 
Route::controller('/', 'AuthController'); // here is defined getLogin
}

With this code I can do a Redirect::to(action('AuthController@getLogin')). Although I'm not sure this is the correct way for achieving this. Any suggestions?

Upvotes: 2

Views: 4891

Answers (1)

damiani
damiani

Reputation: 7371

Attach an auth filter to your admin.example.com group. In the filter, you would check if the user is authenticated; if they are not, then return Redirect::guest(www.example.com/login)`.

Route::group(['domain' => 'admin.example.com', 'before' => 'auth'], function(){
....

In your auth filter:

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::guest( Config::get('app.url').'/login' );
});

After login, you can use Redirect::intended() to get back to where the user was trying to go in the first place.

Upvotes: 1

Related Questions