Table of Contents

Laravel Controllers

Laravel Controllers

A zombie developer does not control situations well since their logic is all over the place, whereas a Laravel developer uses Controllers to separate their logic from the rest of their app.

Let's get control of the situation and add some logic to our app.

Controllers can be looked at as the brains of the operation because this is where all the logic occurs.


Controllers

Models will get/set data from our database, the Views display output in the browser, and the Controller is where the logic happens in our app. Your controllers for your application will live inside of the app\Http\Controllers folder.

Back in section 2, we had a section called Route Closures vs. Route Controller Actions. A Route Closure creates an anonymous function, like so:

<?php

Route::get('/zombie', function(){
    echo 'Welcome to the Zombie Page!';
});

Alternatively a Route Controller maps the route to a controller method, which looks like:

<?php

Route::get('/zombie', 'ZombieController@index');

The example above will execute the functionality in the index() method of the ZombieController.

So, let's go ahead and create a new file inside of app\Http\Controllers and call it ZombieController.php and insert the following code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ZombieController extends Controller
{
    public function index(){
        echo 'Welcome to the Zombie Page!';
    }
}

If we navigate to site.com/zombie we will get the same output of 'Welcome to the Zombie Page!'.

This is obviously a very simple example, usually the logic in your controllers will contain more code than what we did above.

A> You may have noticed it seems like using a Route Closure was a much simpler way of doing things, but believe me, once your app starts to get bigger you will want to put all your logic inside of your Controllers and keep the routes\web.php file as clean as possible. Adding logic to your controllers keeps everything more organized and will make your workflow more efficient.

So let's go ahead and explain the code in the ZombieController.php real quick. After opening our PHP tags we specify the namespace:

namespace App\Http\Controllers

The namespace is the current folder where our class is located, which is inside the App\Http\Controllers folder.

In most controllers we also specify the Request class which will allow us to capture requests from our route (more on this later). We'll talk more about the use functionality in a future section.

use Illuminate\Http\Request;

Next, we give our controller a name and specify that we want it to extend from the default controller:

class ZombieController extends Controller

Finally, we need to specify our index() method and print out our message to the screen:

public function index(){
    echo 'Welcome to the Zombie Page!';
}

This is fantastic! We can map any route to any controller method. This will help keep our route file clean, and it will put the logic in our controller files.

In the next section, we will cover Controllers in a little more depth. You will see the process of execution from the route to the controller and then to the view. Let's continue and learn more about how these pieces come together.