Jensej
Jensej

Reputation: 1305

How to add filter laravel

How add filter, when I logged in, and I enter user/login or user/register on url, I want to redirect to home.

and when I logged out, and I want to protect user/panel

Upvotes: 0

Views: 493

Answers (2)

Hassan Jamal
Hassan Jamal

Reputation: 694

Route.php

Route::group(array('prefix'=>'user','before' => 'isUser'), function () {
    Route::get('register', array('as' => 'register', 'uses' => 'UsersController@create'));

    Route::get('login', array('as' => 'login', 'uses' => 'UsersController@login'));
    // add post routes as required
});

filters.php

Route::filter('isUser', function ()
{
   // I have used Sentry for user Authorisation 

   if (Sentry::check()) {
       return Redirect::to('/')->with('message', "You are already logged in.");
   }
   else{
      // benefit of using Redirect::guest() is that you can use Redirect::intended()
      return Redirect::guest('login');
   }
});

Upvotes: 0

Khan Shahrukh
Khan Shahrukh

Reputation: 6361

Laravel has filters options the official website states the following for the filters

Route filters provide a convenient way of limiting access to a given route, which is useful for creating areas of your site which require authentication. There are several filters included in the Laravel framework, including an auth filter, an auth.basic filter, a guest filter, and a csrf filter. These are located in the app/filters.php file.

Defining a route filter

Route::filter('old', function()
{
if (Input::get('age') < 200)
{
    return Redirect::to('home');
}
});

If the filter returns a response, that response is considered the response to the request and the route will not execute. Any after filters on the route are also cancelled.

Attaching a filter to the route

Route::get('user', array('before' => 'old', function()
{
    return 'You are over 200 years old!';
}));

You can find complete documentation here

Upvotes: 1

Related Questions