Laravel For Beginners: MVC Pattern

By Rasyue | On October 25, 2020

Laravel For Beginners, is a tutorial in which we will learn the basic concepts of Laravel and install composer as well as starting up your first Laravel application.

Let’s get started.

Laravel For Beginners Introduction

Laravel is a PHP framework that employs the architecture pattern of MVC.

If you have never heard of MVC or are confused with this term, allow me to explain.

MVC or Model-View-Controller is an architecture pattern that splits an application into three separate components which are Model, View and Controller.

To explain this further, I am assuming that before trying to pick up Laravel, you would already have picked up PHP and had build a simple web application using PHP from scratch.

Building a website with PHP from scratch, you might have notice that you have the UI part, the database query part all in one file.

With the MVC pattern we can separate those into each of these 3 components and make things easier for you to build your app. Lets further look into these MVC pattern.

Model

Model is one of the MVM in which it its a component that corresponds to any data-related logic. Think of Model as a component that you use to do any process that involves the database.

With Model we can fetch, insert, update data from and into the database and push it to the Controller or the View.

Controller

A Controller is a component that is sort of like a middleware or an interface that works between Model and View. In Laravel, the purpose of Controller is that, any business logic should be done in this component.

For example, with Controller, you can grab a Model Object, use it to grab any data from the database, manipulate it in any way you want to fit your business logic before passing the data to the View component.

View

A View is essentially the component that contains the UI that you display to a user. Yes, a View usually contains all the HTML and stuff that you want to show.

Any data from the database that you needed to generate your HTML will usually be passed from the Model to the Controller and finally to your View.

Next Step

Now that we understand the MVC pattern, let us try to install Laravel, create a new project and create a couple of new pages.

There are multiple ways to install Laravel on your PC but I like to install it using Composer. So, first make sure you have PHP and Composer installed on you PC.

If you have no idea how to install PHP on your PC, I strongly recommend that you install XAMPP.

Laravel For Beginners : Installing Laravel

After you have installed Composer, open up your command prompt and run the following

composer global require laravel/installer

Now, in the command prompt, go to the directory where you wish to create your laravel project, then run the below command

laravel new myfirstlaravelproject

Once that’s done, go into your project’s folder and start your Laravel app.

cd myfirstlaravelproject
php artisan serve

If you do everything correctly, you should be seeing something like this.

laravel for beginners

Congratulations, you just created and run your first Laravel app.

Locating the MVC in Laravel

Now, let’s add a couple of pages, first open your IDE and open your laravel project folder. It should look similar to this.

project-folder

Remember that Laravel employs the architecture pattern of MVC. Now let’s find the folder where Laravel keeps it Model-View-Controller components.

Controller Component – Go to app/Http/Controllers

controllers

Model Component – Go to app/models

model

View Component – Go to resources/views

laravel-pages

Creating New Pages

By now, you are probably thinking that to create new pages, you probably just need to create new files in the resources/views folder.

Well, you are right but there’s more that you need to to be able to go to that page on your browser.

Go ahead, and create two new pages in your resources/views. You can copy the welcome.blade.php and rename it to aboutus.blade.php and privacypolicy.blade.php

I mean, you can name the pages however you like, it does not have to be the aboutus and privacypolicy. Also the HTML for each page is really up to you, you can write any HTML you want or to make things easier just copy the HTML from welcome.blade.php.

Creating Routes

To be able to view or see the pages we just created above, we need to define our routes.

Go to routes/web.php, which looks like below.

laravel for beginners routes

Add below code to the bottom of the existing code.

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

Route::get('/privacy-policy', function () {
    return view('privacypolicy');
});

It should look like something like this.

laravel for beginners routesfinal

Test it out on your browser and see what happens.

The End For Laravel For Beginners

Hopefully this short tutorial will be able to help you to understand Laravel better.

In the next tutorial on Laravel For Beginners, we are going to create authentication in Laravel.

We will learn how can we log in Users with different authentication.

See you in the next tutorial!

Leave a Reply

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

*

*
*