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 />

}<br />

* Catch-all method for requests that can't be matched.<br />

*<br />

* @param string $method<br />

* @param array $parameters<br />

* @return Response<br />

*/<br />

public function __call($method, $parameters)<br />

{<br />

return Response::error('404');<br />

}<br />

Now, every controller that extends Base_Controller (including our own <strong>User</strong>s_Controller)<br />

is a RESTful controller!<br />

But, wait. Now, we'll get a 404 error when we go to http://myfirst.dev/users.<br />

This is because we are not declaring our actions the RESTful way.<br />

Edit your <strong>User</strong>s_Controller class and change the line:<br />

To this:<br />

public function action_index()<br />

public function get_index()<br />

Your <strong>User</strong>s_Controller class should now look like this:<br />

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

{<br />

public function get_index()<br />

{<br />

$users = <strong>User</strong>::all();<br />

}<br />

return View::make('users.index')->with('users', $users);<br />

20<br />

}<br />

Now, when we save and reload the page in our browser it works again! The get_ prefix<br />

that we added to our index method serves much the same purpose as the action_ prefix<br />

that we were using previously.<br />

Unless a method is prefixed appropriately, <strong>Laravel</strong> will not route URLs to them. In this way,<br />

we can ensure that only controller actions are routable and that our web-application's users<br />

can't access other methods that may exist in our controllers by simply typing the names of<br />

the methods in their browsers.

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

Saved successfully!

Ooh no, something went wrong!