Understanding Routing and Views in Laravel

laravel logo

Introduction

Our previous article had you create a blog project with Laravel. Laravel also has an official tutorial for creating a blog, which you may find useful as well. Instead of continuing along with creating the blog project we are going to start a new project. Starting a new project with each article will with learning Laravel through repetition. So now, go ahead and create a new project named portfolio. This project will be a simple website that has a few pages to help you get familiar with routing and editing views in Laravel. If you do not know how, please see out first article in this series called Laravel for Beginners.

Laravel Routing

Routing in Laravel isn’t a very difficult thing to grasp. Routing is basically just dealing with how the URL’s are handled to show different pages to the user. The index has basic information about the website on it, maybe something with news or latest posts. You have other routes or pages the user can view, like a contact page or an about page. You route the user to these pages, which is Laravel lingo for linking the user to the pages from another page. It sounds complicated, and it’s not.

You can find the routing files in the routes folder.

(xenial)fxbg@localhost:/var/www/html/lvel/portfolio/routes$ ls -l
total 16
-rw-rw-r--. 1 fxbg fxbg 528 Apr 15 12:55 api.php
-rw-rw-r--. 1 fxbg fxbg 508 Apr 15 12:55 channels.php
-rw-rw-r--. 1 fxbg fxbg 553 Apr 15 12:55 console.php
-rw-rw-r--. 1 fxbg fxbg 453 Apr 15 12:55 web.php

Above is the directory listing of our routes folder. The default route for our new project is in the web.php file and looks like this:

<?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 a user visits the website, the welcome view is returned. We can change the forward slash to something like /home and when we visit the website and use the /home in the URL we will be presented with the welcome view. However, now the base URL is a 404, broken page.

Laravel routing

Laravel Views

Views in Laravel (or any MVC) is what the user ends up seeing in the browser. These are the front-facing views the user will see when they visit a page. The views have all the markup in them. Laravel uses a combination of HTML and Blade. Laravel’s Blade is the template engine that Laravel uses. A template engine (for those who have never used one) is a way to use variable and loops and things on the front-end of HTML templates. We will cover blade in a later article, for now just know it’s there, and if you choose to, you can go ahead and get familiar with it. The views for the project can be found in the resources folder, in a folder named views.

(xenial)fxbg@localhost:/var/www/html/lvel/portfolio$ ls -l resources/views/
total 4
-rw-rw-r--. 1 fxbg fxbg 2736 Apr 28 23:11 welcome.blade.php

Above you can see the view that comes with the new project by default called welcome.blade.php. Inside this file is the HTML that you see when you visit the base URL, which is just the forward slash, http://127.0.0.1:8000/. We are going to rename welcome.blade.php to index.blade.php and then change the route in the web.php file in the routes folder to index.

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

The Laravel framework will automatically look for the file named index.blade.php in the views folder after changing the route. If you don’t change the route but change the file name, you will be a 404 error. Now let’s create two more files in the views folder. Create a file named contact.blade.php and another file named about.blade.php. After you create these two file modify the web.php file in the routes folder to reflect these two new files (like below).

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

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

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

Now, let’s go ahead and clear the HTML file in the index.blade.php file and add our links to the new routes. I have decided to just rename some stuff and add the links where the old links in the file were, below is part of my index.blade.php file.

    <body>
        <div class="flex-center position-ref full-height">
                <div class="top-right links">
                        <a href="/">Home</a>
                        <a href="/contact">Contact</a>
                        <a href="/about">About</a>
                </div>

            <div class="content">
                <div class="title m-b-md">
                    Portfolio
                </div>
            </div>

laravel indexBut of course, you will want more in the route specification for more sophisticated URL’s, such as when you use forms or paging through some kind of data from the database. Later in the series we will discuss more advanced routing such as accepting arguments in the URL’s to retrieve data from the database. We will also discuss in more detail the blade template engine.

Leave a Comment

Your email address will not be published. Required fields are marked *