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

@else<br />

Looks like we haven't added any users, yet!<br />

@endif<br />

Blade is easy to understand and results in much more elegant code. The {{ }} tags output the<br />

results of the expression within them and replace a typical echo command. Other constructions<br />

such as if(), foreach(), and for() are the same, but without <strong>PHP</strong> tags and with a<br />

preceding @.<br />

Blade doesn't incur a significant performance penalty since it renders<br />

to a raw <strong>PHP</strong> cache. Parsing of a Blade template is only done when<br />

changes have been made.<br />

It's also important to note that you are still free to use <strong>PHP</strong> tags in the<br />

Blade templates.<br />

As you can see we use an if statement to check if the $users array contains data. If it does, we<br />

loop through the users and display a new list item for each. If the $users array doesn't contain<br />

any data, we output a message saying so.<br />

And that's it! Save the file and hit reload in your browser, and you will see the test account that<br />

we created.<br />

This may be a good time to play around with the HTML or add new<br />

users using the test route that we made to get a better feel for how<br />

everything works.<br />

Step 10 – RESTful controllers<br />

We've already mentioned that <strong>Laravel</strong>'s routing system enables you to route GET, POST, PUT,<br />

and DELETE requests to closures. But we haven't talked about how to individually route them<br />

to controller actions.<br />

The answer is RESTful controllers! RESTful controllers enable you to route to different controller<br />

actions based on the request method. Let's configure our application to use RESTful controllers.<br />

Since all of our controller classes are derived from the Base_Controller class, we can simply<br />

add the $restful configuration to it and all of our controllers will be affected.<br />

Update your Base_Controller class to look like this:<br />

classBase_Controller extends Controller<br />

{<br />

public $restful = true;<br />

/**<br />

19

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

Saved successfully!

Ooh no, something went wrong!