Apply filter to all but two routes?

Is there a way to apply a filter (in particular, "auth") to all routes except "login" and "sessions.store"?

There is a small section in the template filters , but I do not know how to undo them.

Something like ASP.NET MVC AllowAnonymous would be nice.

+4
source share
2 answers

Yep - just use route groups like this

routes.php

// Not logged in area
Route::get('/login', ['as' => 'login', 'uses' => 'AuthController@getLogin']);
Route::get('/session', ['as' => 'session.store', 'uses' => 'AuthController@sessionStore']);


// Logged in area
Route::group(['before' => 'auth'], function ()
{
     Route::get('/dashboard', ['as' => 'dashboard', 'uses' => 'DashboardController@index']);
     // Rest of your routes here
}

Or there are other options. You can create a filter based on the class and do something like this (sem pseducode, I haven't tested it, but you get it)

class AdminFilter {

      public function filter()
      {
         if ( ! ((Route::getCurrentRoute() == 'login') || Route::getCurrentRoute() == 'session.store')))
         {
             // If the route is not login or session.store, then run the auth check
             if (Auth::guest()) return Redirect::guest('login');
         }  

         return true;
      }
 }

: - AllowAnonymous, - .

public function __construct()
    {
        $this->beforeFilter('auth', array('except' => array('login', 'sessions.store')));
    }

"" . , ( ) .

+3

app/filters.php , laravel, :

App::before(function($request)
{

});

, , , , .

+2

Source: https://habr.com/ru/post/1530715/


All Articles