Laravel: explain the basic concepts. Part One: Theory

Friends, we have great news. In August, we at OTUS launched a new course - “Framework Laravel” , but there were so many people who wanted to study that not everyone managed to get into the group. A new stream starts at the end of October! We are waiting for everyone and traditionally, on the eve of the start of the course, we share useful material.







In this article we will get acquainted with the theoretical foundations of Laravel - with the framework itself, with its history, and with the basic concepts and concepts that are used in it. This article is intended for beginners who have already mastered the basics of PHP, tried to write their first site, for example, with a small CMS system, but have not yet tried working with PHP frameworks, and did not understand the main advantages that they offer.
So, Laravel is a relatively new web framework (the first release took place in 2011). Laravel took the best from another backend framework, such as Rails, but now Laravel has already overtaken it in terms of popularity. At the time of this writing, the latest version is 6.







Not a framework, but an entire ecosystem



What does it mean? If you had only been involved in Frontend development before, I would compare Laravel with Angular: literally from one line that generates a new project for you, you get almost everything in the box (of course, without the front-end part of the project, but 2-3 more teams and it will turn out to be in place). As far as PHP development itself is concerned, Laravel has almost everything in place that you might want to build a reliable backend application: PHPUnit is a framework for testing, Artisan is a command line interface for database migration, model creation and the rest of the configuration applications, an integrated Homestead server in which you can create a virtual environment, as well as work with routing, processing middleware, as well as comfortable linking view with Blade templates.



We will try to analyze all complex concepts in order. I remind you: this article is only a theoretical introduction for beginners. Practical examples will be in the following parts .



Homestead


The philosophy of Laravel is to give you the maximum number of convenient tools out of the box, and it is uniquely convenient, reliable and easy to use. That is why Homestead is one of the first mentioned in the documentation.



Laravel Homestead is a pre-installed Vagrant - box. What it is? The reader most likely used the assemblies of XAMPP, Abyss Web Server, or downloaded all the pieces necessary for the server to work (Apache, PHP, PHPMyAdmin, MySQL, and much more) independently. You can find the full list of installed inside Homestead here . Why is this done this way? Homestead creates a kind of "virtual environment" that runs in the same environment as on the server and with the same programs. Homestead quickly and easily recreates its “virtual” boxes, and you don’t have to worry for a long time if you accidentally break something.



However, Homestead Laravel does not end there. If you are running Mac OS, you need to try the minimalistic Valet, which does not have so many functions out of the box, but is much easier to install. And also a huge number of additional packages - this is a whole ecosystem!







Database migration



The phrase "Database Migration" sounds most likely somewhat frightening for a beginner. I hope the reader knows what the database is for (in principle, obviously, for storing information) and even wrote in his life something more than “SELECT * FROM Customers” in SQL commands. That should be enough.



So, migration is a kind of control system for transferring your tables to the KB using the table constructor. Migration will allow you to avoid mistakes and conflicts during the construction of tables in the database for a large project together with members of another team. In addition, this will allow you to interact with the database not using tools such as MySQL WorkBench or PhpMyAdmin, but directly from the code, depending on the needs of your project in the data tables. Adding and deleting tables is recorded in the history of migrations, and now at least the timlids will have one less headache.











ORM



ORM is an object-relational mapping system that associates databases with object-oriented programming concepts.



It sounds scary at first glance, but it is directly related to the migration of databases: each class has its own class - a model that is used only to work with this table. This allows not to breed unnecessary work in the database itself, but to interact with it again directly from the project. As a result, it turns out both more convenient and more reliable. Of course, it will take some time to master the commands and features of model generation, but it will take much more to create a large project with a huge number of tables.



Blade - templates: a convenient view of the view



Blabe - templates make it possible to conveniently link your views of the view. Here's what it would look like in native PHP:



<?php include($_SERVER['DOCUMENT_ROOT']."/shop/includes/header.inc.php"); ?> <div class="catalog"> <div class="catalog-filters"> </div> <div class="catalog-products"> </div> <div class="catalog-pagination"> </div> </div> <?php include($_SERVER['DOCUMENT_ROOT']."/shop/includes/footer.inc.php"); ?>
      
      





 <html> <head> </head> <body> @extends('header') @extends('catalog') @extends('footer') </body> </html>
      
      





Agree, it looks much neater! Of course, in fact, this is the controller code and export from the remaining Blade templates, but on a large project it will only be more convenient.







Good assistant Artisan



Artisan is a command line interface that ships with Laravel. It allows you to generate models, controllers, new tests, notifications - right from the command line. This is much more convenient than copying a class template from somewhere or even writing with pens from somewhere.



Application testing



If you want to develop your projects through TDD / BDD (development through testing / development through behavior) then there is PHPUnit in the kit (tests such as Browser / Unit / Feature are supported), as well as a package for generating fake Faker content (useful, for example, when testing the database, because you don’t have to independently fill it with content, which the user usually adds there).



Many pleasant things



Would you like to see authentication through social networks quickly appear in your application? No problem, Laravel Socialite rushes to the rescue. Want to configure caching? No problem, right out of the box. Do you have many projects that are hosted on various services VPS? Laravel Forge in place. Need WebSockets, for example for chat? No problem. And so it can go on for a very long time.



Why should I teach this Laravel?


Now Laravel, no doubt, occupies the position of the most important PHP framework. Now, according to Github, repositories with Laravel are around 195k versus 35k for Yii and 43k for Symfony. This may not be the most honest comparison, but why not try Laravel then?



All Articles