Laravel Routing
A zombie developer reinvents the wheel and creates their own routing system, whereas a Laravel developer leverages the built-in router that is simple to use, extremely flexible, and super efficient.
Using a powerful routing system in your app is crucial for keeping your sanity and preventing brain deterioration. When a user navigates your app they won't run into a dead end; they'll hit the correct "route" instead.
Routing overview
Just to be sure everyone is on the same page, we'll give you a brief run down of app routing.
You can think of a route as being similar to a road. For instance, "We drove down the road (route) to the graveyard." When referring to routes in an application, it is essentially the same. When you type a website URL like site.com/graveyard
, you are telling the browser that the graveyard is the route you want to take. The application then says, "Ok, you want to go to the 'graveyard'? This is the output I have for the graveyard route."
This can be done very easily using Laravel, for instance:
<?php
Route::get('graveyard', function(){
echo 'Welcome to the graveyard!';
});
The code above states that when the browser says 'get' the 'graveyard' route, our app will perform the following function. In the code above our page will display 'Welcome to the graveyard!'.
Couldn't be easier, right?
Let's learn more about the Laravel routing system.
Routing in Laravel
The Laravel web route file is located at routes\web.php
. This is where we will be adding all our routes for our application.
There are 4 basic types of routes we can add. These types are POST, GET, PUT, and DELETE
, and look like the following:
<?php
Route::post('/zombie', function () {
echo "We want to create a new zombie";
});
Route::get('/zombie', function () {
echo 'We want to read or view a zombie';
});
Route::put('/zombie', function () {
echo "We want to update an existing zombie";
});
Route::delete('/zombie', function () {
echo "We want to destroy a zombie";
});
The POST, GET, PUT, and DELETE methods are all part of a RESTful architecture, where each verb corresponds to an action.
POST
When we POST data to a page, we Create an item.
GET
When we GET data from a page, we Read an item or a list of items.
PUT
When we PUT data into a page, we Update an item.
DELETE
Finally, when we DELETE data from a page, we DELETE an item.
This technique is also referred to as CRUD (Create, Read, Update, Delete).
Most often we will use the GET method, but there is also a route we can use to capture any method:
<?php
Route::any('/zombie', function () {
echo "Any request from this zombie route";
});
Awesome!
Ok, how might we access our routes from a browser? Well, like we said, the GET request is usually the method we will use for most requests. If you typed in site.com/zombie we would be directed to the GET method. But, how would we POST data to a route?
Simple enough! We can create a form in HTML that looks like the following:
<form method="POST" action="/zombie">
...
<input type="hidden" name="_token" value="' . csrf_token() . '">
<input type="submit">
</form>
When the user submits the form in the code above the data inside the form will be posted to the site.com/zombie POST route.
You may have noticed the
hidden
input type in the form above, this is a security feature provided to us by Laravel that we will talk about near the end of the book.
How about the PUT and DELETE method? Well, the next 2 methods will need to have a hidden input type to specify that it is a PUT or a DELETE. For example:
<!-- PUT METHOD -->
<form method="POST" action="/zombie">
...
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="' . csrf_token() . '">
<input type="submit">
</form>
<!-- DELETE METHOD -->
<form method="POST" action="/zombie">
...
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="' . csrf_token() . '">
<input type="submit">
</form>
In the previous code sample the forms will send data to the PUT & DELETE route respectively.
Quick Routing example
Let's go through a quick route example of how we might destroy a zombie!
First we need to have a form where we can delete a zombie. Let's say that we have the following code in one of our views:
<form method="POST" action="/zombie">
<input type="hidden" name="id" value="2">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="' . csrf_token() . '">
<input type="submit" value="Destroy">
</form>
The view above will show a button titled 'Destroy'. For simplicity purposes we are just hard-coding an input with an ID of 2, but normally this would change based on the ID
of the zombie you actually want to delete.
Next we need to create our route that will delete that zombie:
<?php
use Illuminate\Http\Request;
Route::delete('/zombie', function(Request $request){
$id = $request->id;
Zombie::destroy($id);
});
And just like that we have destroyed the zombie with an ID
of 2 :) Pretty cool!
Additionally, notice that we specified a Request
variable in the function above. This is simply a class provided by Laravel that allows us to capture request information. But, before we can use the Request class we have to specify the namespace:
use Illuminate\Http\Request;
A> Just FYI, the above example will not fully work since we have not setup our database or models just yet, but we will do that in the next chapter.
In the above route examples, we are using route closures. Let's move on to talk about route closures vs route controllers.
Route Closures vs Route Controller Actions
A route closure is when we return an immediate function containing some code, for example:
<?php
Route::get('/zombie', function(){
echo 'Welcome to the Zombie Page!';
});
Whereas, with a route controller action we specify the controller method we want to run when the route has been requested, for example:
<?php
Route::get('/zombie', 'ZombieController@index');
When the /zombie
route is accessed, our router will call the index()
method inside of the ZombieController
file.
We will talk more about Controllers in the next couple chapters. Just make sure to keep this in mind and it will all start coming together :)
Route Parameters
There will probably come a time when you want to pass parameters to your routes.
As a quick example let's say that we have a zombie with an ID of 5 and we may want to view that zombie by visiting site.com/zombie/5. We may need to pass a parameter to our route:
<?php
Route::get('/zombie/{id}', function($id){
echo 'This zombie has an id of' . $id;
});
The above would print out "This zombie has an id of 5".
So, if we had our models and database all set up we could get the zombie with an ID of 5 and print out his info, which would look like the following:
<?php
Route::get('/zombie/{id}', function($id){
$zombie = Zombie::find($id);
echo 'Name: ' . $zombie->name . '<br />';
echo 'Strength: ' . $zombie->strength . '<br />';
echo 'Health: ' . $zombie->health . '<br />';
});
Above we are getting the zombie with an ID 5 and we are displaying their name, strength, and their health level.
The example above will not entirely work since we have not set up our Models or our database yet. So, with that being said let's move on to talk about our model classes.