An introduction to the Laravel Framework

An introduction to the Laravel Framework

A beginner's guide to using Laravel to build robust web applications

·

6 min read

Laravel is a web framework that helps you build web applications. You will be able to build your whole app without ever leaving the Laravel ecosystem. This is especially nice for people who are coming from a PHP background. Laravel has a whole array of documentation, and video tutorials that can help even a beginner to learn the nitty gritty of Laravel in a very short period. In this guide, you will learn the differences between websites and web applications, you will also learn what the Laravel framework is, you will also learn how to set up your first Laravel project and the different user interface libraries and frameworks you can use alongside Laravel.

Prerequisite: To get the most out of this guide, you should have a basic understanding of HTML, CSS, JavaScript and PHP. If you are not familiar with this technologies, it is recommended to familiarize yourself with the technologies.

Difference between a website and a web application

Websites and web applications are so alike that sometimes it might even be hard to differentiate between the two.

I. What is a website?

Websites are built by programming languages like HTML, CSS, and JavaScript. Even more modern languages and frameworks, like React, NextJs etc. Websites most of the time provide information to the user. Information on the website could be the details of a product or the about us of a business that owns the website.

II. What are web applications?

Websites are similar to websites but with extra functionalities. These functionalities can include a cart, a submit contact form button, an online word-processing platform, etc.

III. What do web applications entail and how to build them?

Web applications allow you to build software that works in the browser against creating desktop applications. In clear terms, web applications are apps that work on the web. This helps solve the problem of cross-platform compatibility. The advantage of web applications is that you do not have to worry about creating software for each platform i.e. Windows, Mac and Linux.

There are many different technologies and web frameworks to help build web applications but in this guide, we will be focusing on how to use the Laravel framework to build web applications.

Introduction to the Laravel Framework

I. Why we can use Laravel to build web applications

Laravel helps build a full-stack application. You can build your whole app without ever leaving the Laravel ecosystem. This is especially nice for people who are coming from a PHP background. Laravel has a whole array of documentation, and video tutorials that can help even a beginner to learn the nitty gritty of Laravel in a very short period.

II. Understanding the MVC architecture of the Laravel Framework

Laravel follows the Model-View-Controller architecture. The application structure separates the logic (model) from the presentation (view). The controller is like the middleman that mediates between both.

Setting up your first Laravel Project

As of the time of writing this guide, Laravel's latest version is the Laravel 10. Laravel 10 was released on February 14, 2023.

When you want to set up your Laravel project, there are a couple of things you must have depending on your device and operating system.

In this guide, we will be more focused on the Windows Operating System.

Your local machine must have PHP and composer installed. Composer is a package manager for PHP, just as NPM is a package manager for JavaScript. If you do not have Composer installed on your local machine, you can get it on the official Composer website.

In addition, you need to have nodejs and NPM installed as well. You will need them to install some packages on your machine as well. You also need to download Xampp. It makes your work a whole easier. Xampp comes bundled with PHP. So you do not have to worry about installing PHP on your machine.

After installing Composer and PHP on your machine, you can create a project by using the creat-project command. Open your command prompt (in Windows OS), navigate to your Xampp directory, then go to the htdocs folder, inside the htdocs folder, you run this command:

composer create-project laravel/laravel first-project

It will create the 'first-project' in the folder. You can then open the project with your desired code editor. I use VSCode as my favorite code editor.

You can visit the official Laravel documentation to learn more.

I. Environment variables in Laravel

Your Laravel project has a .env file. This is where you have all of your environment variables that make your project work. Here you can include API keys for your project, and other sensitive credentials, then call them later in your code. This helps to hide sensitive credentials from hackers.

II. What are controllers, models and views and how to use them

Like I said earlier when I was describing the MVC architecture of the Laravel Framework. The model holds the logic of the application, while the view is the interface (more like the front end of the web application), and then the controllers bind/connect the model with the view.

You can create models by running this command in your terminal

php artisan make:model

III. Working with routes

In your Laravel project, there is a route folder, inside the route folder, there is a file web.php, inside this file you can specify a particular route that will load when a user visits your application. For example, if the home page of your application is your welcome.blade.php (I know this file type might look strange to you, I'll explain more about it in the next section). You can set the route of the homepage to be:

Route::get('/', function () {

return view('welcome');

});

How to choose different frontend frameworks to work alongside Laravel

The beauty of Laravel is that you can use any frontend library or framework you are comfortable with coupled with the Laravel backend to build your robust web applications. Most people coming from the JavaScript ecosystem, can easily make use of React/VueJS/Svelte, and then serve Laravel as an API to the backend.

For most of us coming from the PHP ecosystem, however, there is the opportunity to use the blade templating system without even using any JavaScript framework. Laravel even has Livewire that behaves like JavaScript, so you can write full-fledged web applications without even writing a single line of JavaScript. That's how versatile and comfortable Laravel can be.

Laravel is for everyone. It doesn't matter from what technological background you are coming from, there is something comfortable for you.

Here are the different frameworks and UI libraries you can make use in the Laravel Framework:

I. Blade templates

Blade is a templating engine that is included with Laravel. Blade allows you to write PHP code in your templates. Blade template uses the .blade.php file extension and is stored in the resources/views directory.

II. Livewire

Livewire is a framework that helps build dynamic and modern user interfaces without writing JavaScript.

Livewire allows you to write HTML attributes that connect with your backend. It's like a medium between your application's front end and back end.

We have different HTML attributes such as wire:click that allow you to do something on the fly when you click on a button.

III. React/Vue

Many developers who are coming from the JavaScript ecosystem might prefer to use modern JavaScript frameworks like React/Vue. Laravel has Inertia that bridges the gap between these JavaScript frameworks and Laravel.