Get up to 80 % extra points for free! More info:

Lesson 1 - Introduction to Laravel framework for PHP

Welcome to the first lesson of the course for creating web applications in the PHP framework Laravel. Laravel is a complete framework for PHP that greatly simplifies the creation of web applications. It is a well-designed object-oriented and at the same time the most widespread PHP framework in the world. Thanks to its popularity, there are also a lot of instructions and extensive official documentation. Small and even large projects are built on it, some of them can be found on this page. It is also worth mentioning that the basic building block of this framework is the Symfony framework, but its knowledge is not necessary, as the two frameworks are quite different.

Laravel is based on simple and clean code. Laravel is notable for being optimized for real web applications. This means that everything is already ready and components can be generated using simple commands in the terminal. After you'll learn this framework, creating a website will take a fraction of code unlike pure PHP.

In today's lesson, we'll explain why we should use PHP frameworks in general and talk about how Laravel works.

Why use the framework

The framework is a comprehensive set of dependent libraries. You can't do serious projects in PHP without quality libraries. There are two reasons for this.

  • Although PHP is a very high-level programming language and contains a lot of useful features, over time you will start to encounter gaps in its standard libraries. Many important functions are missing or very poorly worked with. Once you've found that you've reprogrammed a database class, form handler, or table paging in each project over and over again, you'll have to think of creating simple libraries for these basic activities.
  • If you program in pure PHP, about 50% of the code is unnecessary ballast. Using quality libraries, you can write an application in half the time and with half the number of lines. The application will be easy to maintain and you will enjoy its creation. You will not always invent the wheel and solve the same routine issues (such as how to verify whether the user is an administrator), vice versa, you will develop interesting functions of the application and that is what programming is all about.

You can either create your own framework or use one ready-made, which is Laravel.

Necessary knowledge

Laravel is mainly a large set of reusable components, on which the framework for creating web applications is built. When you program in the Laravel framework, you still program in PHP. In addition, you use object-designed components on the principles of the MVC architecture. Therefore, to use the Laravel framework, you should have advanced experience with PHP, know well object-oriented programming and at least the basics of MVC architecture. Learning Laravel without this knowledge is a waste of energy, because you probably don't understand its principles and you just lose your time. If you are unfamiliar with OOP or MVC, please read these two local courses first.

For diligent, I recommend to study software design so that you can understand certain practices that are used in this framework.

How Laravel works

At the beginning we will describe the basic terms and ways how Laravel works.

MVC

As already mentioned, Laravel is an MVC framework, so it's probably good to review what that actually means.

The application consists of parts of three types, which are divided into 3 basic tasks:

  • control,
  • logic,
  • output.

Only such a divided application is clear and easy to expand.

  • Controllers (control) - The controller is the part which the user communicates with. The user passes the parameters to controller and it returns the data to the user (e.g. a HTML page). The controller typically passes parameters to the models from which it obtains data. This data are passed to views (templates), which incorporate the data into some HTML code. This HTML code is sent by the controller to the user's browser. Controller acts as such an intermediary.
  • Models (logic) - The models contain application logic such as. Work with databases or calculations. Each data entity usually has its own model (user, article, comment, ...).
  • Views (output) - The views contain Blade templates (HTML code). Blade is a template system that, unlike other systems, does not forbid the use of PHP code in the view, so it is not only used to insert variables into HTML code. Blade uses a special syntax, which is then converted to normal PHP code, and this generated view is then cached for future use for faster loading (unless the view is modified).

Routing

Before the user gets to the controller, he encounters so-called routing. The task of this layer is to find out what the user wants from the URL address and to call the appropriate controller to handle his query.

Routes can be defined in several ways. The requested action can only be performed by a simple anonymous function defined by us, or the controller will take care of the given action. Laravel can even generate actions for us, which will probably be needed by each of our data entities (article, user, category, ...). Those who know more probably already know that I have CRUD (Create, Read, Update, Delete) on my mind, but we'll talk about that later in this series.

Life cycle

You need to be sure of what is going on inside when using the framework otherwise you are degrading from a programmer to an experimenter. Let's describe the life cycle of the application in the Laravel framework - i.e. what happens when the user enters something in the address bar, for example:

shop.com/book/harry-potter

The life cycle of a page is simply shown in the image below. We'll describe it right away.

The page cycle in the Laravel framework shown by a diagram - Laravel Framework for PHP
  1. The request gets to the routing first. It is determined what the request is (and which controller and its method it is aimed at) and what everything should be checked and happen before the data are passed on using the set of rules for the route itself.
  2. The request is filtered through the so-called middleware classes, which can be defined before the routing itself, or even in the controller. In our case it may be user authentication and its rights to view books. Or it just check if the passed parameter does not contain characters that we do not want to have there. You can think of middleware as a security guard that will check you before entering.
  3. According to the address, the framework already knows that we want something with books. Therefore the BookController and its show() method is to display the book, because the given address meets the configured route book/{book}. The controller get the parameter {book} from the URL, i.e. some book identifier.
  4. It is now determined what all the parameters of the action method require. For example, we can pass a special class using dependency injection, which validates the submitted data (form). However in our case it will only be an instance of the model.
  5. Subsequently, the controller obtains an instance of the Book model with data from the database using the passed identifier "harry-potter".
  6. BookController passes data from the model to the view (template).
  7. The template contains an HTML page for displaying the book and some Blade syntax for listing the data of the book. The resulting HTML page is generated using the Blade engine.
  8. BookController gets back from the resulting HTML template and sends it to the user in response.
  9. The user will see an HTML page in the browser and have no idea about everything we have described here :)

If you don't fully understand some of the steps now don't despair. We will gradually explain everything in the course. It's just a matter of understanding what's going on in the background and how complex one requirement can be. With the correct use of framework components, a parameter can easily go through dozens of classes before it even reaches the controller.

What else you should know

  • Each controller has typically several templates for different methods (detail, addition, list, ...).
  • In most cases, each model represents one database table. Each record in the database table is an instance of the model with the values passed from the database.
  • Each part of the logic has its place in the Laravel framework. We have already outlined the middleware classes a bit. We can also have a class for validating the submitted data (e.g. via a form), a class for checking specific user rights, etc. We will talk about most of these classes during the series, but they are all described in detail in the official documentation.

That would be all for an introduction.

In the next lesson, Installation of Laravel and first project run, we will launch the first project in the Laravel framework.


 

All articles in this section
Laravel Framework for PHP
Skip article
(not recommended)
Installation of Laravel and first project run
Article has been written for you by Lishaak
Avatar
User rating:
2 votes
Author is interested in programming mostly at web development, sometimes he does funny video edits from his vacations. He also loves memes and a lot of TV series :)
Activities