Table of Contents

Piecing It Together

Piecing It Together

A zombie developer may write all the logic and views in one file, whereas a Laravel developer uses routes, models, views, and controllers to piece their app together making it cleaner, more flexible, and better organized.

Leveraging all the different classes and functionality that Laravel offers will make our application more flexible. By separating our functionality in routes, models, views, and controllers, our code will be a lot cleaner and more enjoyable to work with.


Piecing It Together

In the last couple sections, we talked about Routes, Models, Views, and Controllers. Now, let's go through an example of "Piecing those together" and show you how we might display a list of all our current zombies using routes, models, views, and controllers.

When we go to our site.com/zombies route, we want to be able to see all our current zombies. Here is the process that we will need in order to accomplish this. We will need to:

  1. Create a route that maps to a controller method
  2. Create our controller
  3. Retrieve the zombies from the zombie model in our controller
  4. Pass our zombie data to our view
  5. Output our zombie data in our view

1. Create a route that maps to a controller method

This is fairly straightforward, we simply need to create a route inside of our routes\web.php file that links to a controller method:

Route::get('zombies', 'ZombieController@show');

2. Create a controller

We can use the same zombie controller that we created at

App\Http\Controllers\ZombieController.php which looks like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;

class ZombieController extends Controller
{
    public function show(){
        // Show our zombies
    }
}

Above you can see that instead of the public function index() we used public function show() because show() is the method that we used in our route. We specified that when a user visits the zombies route we want that to point to the ZombieController and the show method.

3. Retrieve the zombies from the zombie model in our controller

Ok, inside of our Zombie Controller we need to get all our zombies from our Zombie model. We will add this code inside of the show() function and store it in a variable like so:

    public function show(){
        // Show our zombies
        $zombies = Zombie::all();
    }

Great, we've stored all our zombies from our zombie model inside of the $zombies variable.

Before we go to the next step, I want to point out that our app will not know where to find Zombie::all().

If you go back to section 5 where we created this zombie model we put it inside of the App namespace since it is in the app folder, so we would need to call App\Zombie::all(). Or we could just tell our controller which zombie to use, like so:

use App\Zombie as Zombie;

And now, Altogether our controller would look like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;

use App\Zombie as Zombie;

class ZombieController extends Controller
{
    public function show(){
        // Show our zombies
        $zombies = Zombie::all();
    }
}

4. Pass our zombie data to our view

Passing our zombie data to our views is very straightforward. Inside of the show() function, we need to load a view file and pass along the zombie data. To load a view file we can return a view function at the end of our method like so:

return view('zombies', array('zombies' => $zombies) );

The first argument is a string with the view we want to load (located at resources\views\zombies.blade.php) and the second argument is an array of data that we wish to pass to our view.

Quick note: PHP offers a function called compact that will convert all parameters past to it into an array with the specified variable, so we could simplify the code that we pass to the view by changing it to this:

return view('zombies', compact('zombies') );

So, all together we would want our show() function from our controller to look like the following:

public function show(){
    // Show our zombies
    $zombies = Zombie::all();

    // Load the view and pass it our data array
    return view('zombies', compact('zombies') );
}

Simple enough, now inside of our resources\views\zombies.blade.php we will have an array of zombies stored in a variable called $zombies.

The last thing we need to do is to display our zombies in our views.

5. Output our zombie data in our view

Inside of our zombie view file located at resources\views\zombies.blade.php we can add a basic HTML page with an unordered list and loop through each of our zombies like so:

<html>
<head>
    <title>Zombies</title>
</head>
<body>

    <ul>
        @foreach($zombies as $zombie)
            <li>{{ $zombie->name; }}</li>
        @endforeach
    </ul>

</body>
</html>

In the example above we do a simple foreach statement and loop through each of the zombies and list out their name.

Also, notice that above we are using blade syntax that we covered previously.


There you go! That was a fundamental overview of how each piece will work from your route, model, controller, and view.

Pretty fun stuff, right!

Now that we have a basic overview of how our app works let's move on to talking about an excellent helper tool in Laravel called artisan that will make our lives much easier.