In Laravel 5, how to implement the App :: before () filter?

In Laravel 4.2, I had this before the filter, which set the model and Eloquent table based on the URL (admin.example.com vs example.com)

Here is my filter code:

App::before(function($request)
{       
  // Check if we are using the admin URL
  $host = $request->getHost();
  $parts = explode('.', $host);
  if ($parts[0] == 'admin')
  {
    // Set the config for user info (not sponsor)
    Config::set('auth.model', 'Admin');
    Config::set('auth.table', 'admins');
  }
});

I tried to create middleware for this in laravel 5 and have this code:

class AdminOrSponsor implements Middleware {

  public function handle($request, Closure $next)
  {   
    $host = $request->getHost();
    $parts = explode('.', $host);
    if ($parts[0] == 'admin'){
        Config::set('auth.model', 'Admin');
        Config::set('auth.table', 'admins');
    }

    return $next($request);
  }

}

In my routes.php file, I install a controller that is called based on the auth.model parameter, for example:

Route::get('/auth/login', Config::get('auth.model') . 'Controller@getLogin');
Route::post('/auth/login', Config::get('auth.model') . 'Controller@postLogin');
Route::get('/auth/logout', Config::get('auth.model') . 'Controller@getLogout');

I found that all routes were read before middleware, so the change that I am trying to do through Config :: set () does not occur. I get only the value set in the auth.php configuration file.

What am I doing wrong and how do I do this in Laravel 5?

+4
1

, .

, , , unit test, . , , $_SERVER ( unittests).

:

  • :

    Route::get('/auth/login', 'AuthController@getLogin');
    Route::get('/auth/login/admin', 'AdminsController@getLogin');
    Route::get('/auth/login/sponsors', 'SponsorsController@getLogin');
    
  • , AdminsController .

  • AuthController auth/login , .

"" laravel, , - .

+1

All Articles