11.08.2017 Views

codebright

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

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

Build An App 1: Playstation Game Collection 369<br />

13 |<br />

14 */<br />

15<br />

16 // Bind route parameters.<br />

17 Route::model('game', 'Game');<br />

18<br />

19 // Show pages.<br />

20 Route::get('/', 'GamesController@index');<br />

21 Route::get('/create', 'GamesController@create');<br />

22 Route::get('/edit/{game}', 'GamesController@edit');<br />

23 Route::get('/delete/{game}', 'GamesController@delete');<br />

24<br />

25 // Handle form submissions.<br />

26 Route::post('/create', 'GamesController@handleCreate');<br />

27 Route::post('/edit', 'GamesController@handleEdit');<br />

28 Route::post('/delete', 'GamesController@handleDelete');<br />

Wait, what’s that Route::model() method? Well here’s a lovely piece of black magic that Laravel<br />

has to offer. The Route::model() method will let Laravel know that any route parameters with the<br />

same name as the first parameter to the method will be an Eloquent model instance defined by the<br />

second parameter. So in the above example, any route paramers named game, like the one within<br />

/edit/{game} will be bound to Eloquent Game model instances.<br />

Laravel will look at the integer supplied as a parameter, and perform a lookup on the model by<br />

primary key. It will then pass the appropriate model instance as a parameter to the controller action.<br />

How useful is that? The answer is… VERY!<br />

You should be more than familiar with the rest of the routing process by now. We have four GET<br />

routes used to display our application pages, and three POST routes to handle form submissions.<br />

Let’s make sure that everything is working as expected. We will use the Artisan serve command to<br />

fire up a basic webserver and test our application.<br />

1 $ php artisan serve<br />

2 Laravel development server started on http://localhost:8000<br />

We see that our development server is running on port 8000, so let’s visit the http://localhost:8000<br />

to ensure that we receive our blank template. It works great for me! Onwards to templating.<br />

Views<br />

For our templates we are going to use Twitter Bootstrap. It’s a CSS framework that will handle the<br />

styling of typical application HTML for us. I find that it’s great for prototyping applications. We will

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

Saved successfully!

Ooh no, something went wrong!