loading...
Cover image for Laravel Tutorial #2: Routes, Views, Controllers and Models

Laravel Tutorial #2: Routes, Views, Controllers and Models

ericnanhu profile image Eric Hu Originally published at techjblog.com ・4 min read

In section 2, we’ll talk about routes, controllers, views and models, and their relationship with each other.

Routes

The most basic routes in Laravel accepts a URL, and then returns a value. The value could be a string, a view or a controller. All the routes are stored in routes/web.php:

web.php<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

When the Laravel route receives “/“, it returns a view called “welcome“, which is at resources/views/welcome.blade.php.

Open the browser, go to http://localhost, you will get this

img

You can change the content inside welcome.blade.php, and then refresh the browser, see if anything changed.

Also, we can make it return a string:

web.phpRoute::get('/', function () {
    return 'Hello, World!';
});

When the Laravel route receives “/“, instead of a view, it returns the string “Hello, World!“,

Refresh the browser,

img

Or we can make it return a controller, which points to a view. But before we do that, let me first explain what controllers are.

Controllers

A controller is a place where you organize the behaviours of your app. For example, when the router receives “/“, it returns a “IndexController“.

The “IndexController” retrieves required information from the database, and then put them in the corresponding location of a view, and finally returns the view to the browser.

This is the most basic use of a controller. Of course, it can do a lot more than that, and I will talk about them as we encounter specific problems later in the tutorial.

Now, we can create our first controller. In the terminal, type in:

php artisan make:controller IndexController

Go to app/Http/Controllers/, and you will find a IndexController.php

IndexController.php<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class IndexController extends Controller
{
    //
}

We can try to make it return a string by creating a function inside the class:

IndexController.phpclass IndexController extends Controller
{
    //
    public function index()
    {
        return 'this is a controller';
    }
}

We created a function called index(), and inside the function, it returns a string 'this is a controller'.

In order to test it, we need to make a router return the “IndexController“. Go back to web.php, and create a new router like this:

Route::get('index', 'IndexController@index');

Notice the second argument is no longer a function, it is 'IndexController@index', which means go to the IndexController, and find the index() function, execute whatever is in it.

In the browser, go to http://localhost/index

img

We can also make the IndexController return a view.

IndexController.phpclass IndexController extends Controller
{
    //
    public function index()
    {
        return view('welcome');
    }
}

Refresh the browser:

img

Views

Let’s reexamine the view welcome.blade.php, you will notice it is just an HTML file. Yes, views are based on HTML and CSS since they are what you see in the browser, but things are slightly more complicated than that.

If it contains only HTML codes, the entire blog would be static, and that is not what we want. So the view would have to “tell” the controller what information to retrieve and where to put them.

We will talk about these in the later sections of this tutorial.

Models

Remember I said controllers are in charge of retrieving data from the database? Well, that’s where models come in.

Each database table has a corresponding “Model”, and that model will handle all the interactions with the table.

But before we create models and dynamic views, we need to design our database structure.

Summary

img

In summary, when you type in the URL in the browser, the router reads the information and returns the corresponding controller, the controller would retrieve the data from the database through the model and put them in the corresponding view, and finally, the view is returned to the browser.

Since this tutorial is for beginners, I would try to explain the concepts using the simplest words possible. If you still have questions, please comment below.

Next Post: Laravel Tutorial #3: Database Structure

Related Articles

How to Make Your Server More Secure

Laravel Tutorial For Beginners

Django Tutorial For Beginners

Posted on by:

Discussion

pic
Editor guide
 

If your controller will only have a single method, you can use the __invoke() method instead and just pass in only the controller Route::get('/index', 'IndexController'); instead of the controller method pair, gotta love invokable controllers. 🤘