Dwight Watson's blog

Skipping controller filters in Laravel 4

This blog post was originally published a little while ago. Please consider that it may no longer be relevant or even accurate.

Filters are a great way to introduce controller-level checks before every request in your controller without repeating yourself. You might often find that your BaseController has $this->beforeFilter('auth') right there in the constructor, to ensure that only logged-in users have access to your applicaiton. You might also use something like $this->beforeFilter('csrf', ['on' => ['post', 'put', 'patch', 'delete']]) to check the CSRF token on any non-idempotent requests (in case you didn't notice, I'm a fan of registering my filters in controllers rather than the routes.php file).

However, what if a filter you registered in your BaseController isn't going to work in one of it's child controllers. For example, you want your entire application to have the auth filter, but that isn't going to work on your SessionsController which manages the login process. Fortunately, it's really easy to "forget" filters in your constructors to make sure they don't interfere with the expected response.

class BaseController extends Controller {
public function __construct()
{
$this->beforeFilter('auth');
}
}

class SessionsController extends BaseController {
public function __construct()
{
parent::__construct();

$this->forgetBeforeFilter('auth');
}
}

It's also worth noting that the same thing exists for after filters, with the forgetAfterFilter() method.

A blog about Laravel & Rails by Dwight Watson;

Picture of Dwight Watson

Follow me on Twitter, or GitHub.