21.11.2014 Views

Laravel Starter - PHP User Group (Myanmar)

Laravel Starter - PHP User Group (Myanmar)

Laravel Starter - PHP User Group (Myanmar)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Laravel</strong> <strong>Starter</strong><br />

In this example, we're only using one key/value pair for configuring our route. We use the key<br />

before to tell <strong>Laravel</strong> that our route uses a before filter. The value associated with the before<br />

key is the name of the filter, which should be run before our anonymous function is executed.<br />

Controllers are groups of routable methods that are similar and are therefore uniquely<br />

convenient to filter upon. Often the same set of filters that is appropriate for one action<br />

in a controller is appropriate for the rest. Filtering at the controller level gives you more<br />

flexibility and less redundancy than defining your filters in each route declaration. Let's<br />

look at securing our users controller.<br />

class <strong>User</strong>s_Controller extends Base_Controller<br />

{<br />

public function __construct()<br />

{<br />

parent::__construct();<br />

}<br />

$this->filter('before', 'auth');<br />

Here we're looking only at the top-most section of our users controller. The rest of the<br />

controller is identical to what we created in the Quick start section.<br />

As you may have noticed, we've declared a constructor for our <strong>User</strong>s_Controller class.<br />

A constructor is a method that is run immediately once our class is instantiated as an object.<br />

We use a controller class' constructor to define filters for that controller's actions. It's also<br />

important to notice that we first call the parent::__construct() method. It is important<br />

for <strong>Laravel</strong>'s Controller class to have its constructor executed so that it can initialize itself<br />

and be ready for action.<br />

40

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!