We may need to perform some actions before the controller function get called.

For example

Check user authentication before access to certain page.

AccountController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
class AccountController extends BaseController
{
...

public function __construct()
{
parent::__construct();

// before goes into certain function, check for authentication
$this->beforeFilter('auth');
}
}

The filter function is in

app/filters.php

1
2
3
4
5
6
<?php
...
Route::filter('auth', function()
{
if (Auth::guest()) return Redirect::to('login');
}

References: