This article is intended for novice web developers, as well as those who want to understand why it is worth exploring the PHP-framework Laravel and what ecosystem it offers us. The article was written at the time of the relevance of Laravel version 5.4 , in August 2017 the release of Laravel 5.5 will be released, which provides even more features.Content:
')
Introduction to web development: what was before and what is today
In this article, I would like to talk about what the Laravel PHP framework is and how, thanks to this framework, a novice web developer can expand his horizons, as well as raise his cultural level of development. Let's try to reveal the theme of creating a web project and help beginners.
An engineer, programmer or web developer?If 30 years ago, the engineer and programmer was one and the same person, and it was safe to say that he could even repair electronics, today, thanks to technical progress and the development of technology, we have a lot of programming languages, specializations and areas for which one person just physically does not have enough time. Most likely, your neighbor programmer is not able to repair the smartphone, although he may write an application for this smartphone.
Today there are a lot of developers with different levels of knowledge: there are professionals with extensive experience and knowledge of at least several programming languages ​​and development cultures, and there are beginners with big ambitions and a desire to develop.
It's no secret that PHP is considered a programming language for development that requires a minimum set of knowledge. This is a programming language with a very low threshold of entry.
Literally anyone can take and immediately display the line on the screen. That is why experienced developers in any programming language consider PHP developers to be “fake” developers, and PHP as “fake” programming languages.
But is it possible to create a serious product in PHP and how to prove to others that PHP can be trusted? If you are one of those people who consider PHP to be a “frivolous” programming language, then I advise you to read it to the end and, most likely, you will change your mind.
Development of any software product always requires more than just knowledge of the development language itself. To create and maintain it, a developer needs a lot of other knowledge and skills.
We will talk about the development of a web project and the fact that today the web developer needs to know to successfully launch a web project, and most importantly - I will try to show that
Laravel is the perfect solution for those who want to quickly and competently create a safe and reliable web project, while always staying at the height of web development technologies.
Start creating a web project
Before you start creating a web project, you need to think not only about the structure of the project and its features, but also about the development process and, most importantly, about your working environment.
Although Laravel itself is a PHP framework, you should not underestimate it, because it is a whole ecosystem for web development.
If you downloaded the Laravel framework in a
.zip archive to evaluate its capabilities, then your working environment is finely tuned incorrectly, and you have a gap in knowledge. You definitely should carefully read this article to the end.
Stage one - the process of writing code
You can work on any operating system, including Windows. We need a good
IDE (Integrated Development Environment). I recommend
PhpStorm . You can use the text editor
Atom or
Sublime Text . Of course, you can write code in a regular notepad, for example,
Notepad ++ , but a good IDE is an indispensable thing.
Also, after installing an IDE or text editor, I advise you to spend time and explore as much of its features and functions as possible. This will help save a lot of time in the future, as well as automate many routine tasks.
Many people believe that "cool" developers should write code in a notebook, but remembering function names from memory is one thing, and not to make typos in the code, to simplify and speed up the development process is quite another. The main task is to master all the features of IDE.
In addition to the IDE, we will need to install
Composer , it is through it that we will install (update) Laravel, add (update) additional packages to our web project.
Be sure to learn how to work with Composer, this is a very important and useful tool.
See the Laravel installation instructions at
this link for details.
Further, we will not describe the process of writing code, but suppose that you have already installed IDE and Laravel.
After installing Laravel, the display of the base page is immediately registered in the code - this is enough to go to the next part of the article.
Stage Two - Code Testing
To test a web project, you do not need to upload files to an
FTP server , install local
Apache (the same
Denwer or
XAMPP ) - you did this many years ago, and many newbies do this until now. This is wrong and will not save from errors in the code. To date, these tasks have the appropriate tools that will save a lot of time and nerves.
Laravel offers us to install Homestead.Homestead is a Ubuntu operating system image that already has everything you need.
The process of installing and configuring Homestead can be found
at the link .
To install the image, we need
Vagrant and
VirtualBox . Thanks to this image, you will know exactly which modules you need to install and how your Ubuntu code will behave. You can also install any additional software.
In short, you will have shared folders with the code in your system, which will be available inside the Ubuntu image, and your code will be executed inside Ubuntu.
In the browser you type
site.app , and you have a site from Ubuntu. At the same time, you will also have access to Ubuntu via SSH.
For beginners, installing and configuring Homestead will take time, but as a developer you simply must do it.
It is worth noting that Homestead can be installed not only on Linux, but also on Windows.Next, we assume that Homestead is installed, and the site with a fresh version of Laravel opens in your browser.
Your code runs in the browser, but does everything really work?No self-respecting developer should write code without tests. Tests allow us to be absolutely sure that everything works as we had in mind. Take the time to write tests. Each professional developer must write tests of his code.
Laravel offers us the tools to fully test the web project from all sides. You can test everything: create a temporary database, check the completion of HTML forms, check file uploads, even the contents of PHP sessions and send emails.
Laravel was created for the qualitative testing of all the capabilities of your project.
Testing documentation can be found at
this link .
In Laravel, tests are located in the
tests folder and are executed by the
phpunit command in the console, or directly from the IDE.
Tests are of several types:
- Functional - Feature Tests
- Modular - Unit Tests
Feature tests - functional tests.Tests that check the functionality of a web project, for example: user registration, sending notifications, filling in web forms, downloading files. They allow us to check which data is displayed in the browser. Now you do not need to fill out web forms manually to find out if they are working.
You can also test using Laravel Dusk, not just sending HTTP requests, but using the real Chromium browser engine.
This will help us
Laravel Dusk .
Unit tests are unit tests.Another type of testing is unit testing. These types of tests check the logic of our application, each function, catch events, determine whether a letter has been sent, check text of the letter, check whether a task has been added to the message queue, and anything that might break if you or someone else unsuccessfully change your code. .
Each project function should have its own tests, and when you complete a project, all tests should run successfully.
If you change the functionality, you can add tests. This will save you and your colleagues from mistakes and help you more easily diagnose the problem.
Unit testing avoids errors in the application logic.
It is worth noting that there is a methodology for developing
TDD (test-driven development) - development through testing. First we write the tests, and then we gradually implement the code. When all the tests are completed, we can say that we have finished writing the code.
If you have not written tests for your projects, then it’s time to move to a new level. In addition to tests, there are other helpers for analyzing the performance of a web application.
Laravel offers us to install the
Laravel Debugbar .
This is a special package that is displayed on your site in debug mode. Using it, you can track all SQL queries to your database in order to further optimize them.
Stage Three - Build Project
After creating a web project and passing tests, we need to prepare our project for placement on the server.
Laravel provides us with
Laravel Mix .
Laravel Mix uses
Webpack and can work with
CSS, JS, Less, Saas, Stylus, PostCSS .
This is a great tool that, using a special module builder Webpack, brings together all our JS and CSS files, and, most importantly, is able to create versions of these files.
Thus, each build of our project allows you to have different names of JS and CSS files in the HTML code, which solves the problem with caching when the content of the file changes.
In the template of our project we write:
<link rel="stylesheet" href="{{ mix('/css/app.css') }}">
After assembly, it turns into:
<link href="/css/app.289df32d2d2c47df3b16.css" rel="stylesheet">
In this case, the visitor's browser immediately downloads a new file from the site.
Isn't it convenient? Similarly with JS-files.It is worth noting that Laravel works wonderfully with the progressive
Javascript framework Vue and makes it very convenient to create web applications based on this JS framework. In addition, each component can be conveniently placed in a separate file.
How to write components for Vue using Laravel can be found at
this link .
Stage Four - Deploy (deploy) Code
Usually, after the project is assembled, its files must be uploaded to the server and the table structure in the database must be updated.
We take a folder with files and upload to an FTP server.
Go to phpMyAdmin and make changes to the database.We will not use
FTP and
phpMyAdmin , otherwise as long as we make changes, all users who visit the web project site will see a lot of errors about the absence of some files or fields in the database.
We can, of course, announce the conduct of technical work, showing our users how technically complex our project is, which requires a complete shutdown, but it definitely will not please anyone.
There is a very simple and competent solution that allows you to update the web project code without disconnecting it, and no user will receive an error message.
The first thing we need to learn is
Git .
Git is a distributed version control system for files.With Git, you can track changes in files, return the old version of files and work in a team on the same code, without confusing anything.
You can use
Git through the service.
You can create either a public code or a private one (for private repositories, it is paid).
You can also use another free
service BitBucket , which allows you to create private repositories with code for free.
In addition, Git itself can be configured so that when making changes, certain actions take place:
- launch of project tests through Travis CI ;
- code formatting according to the standard ;
- code quality analysis through the tool .
Thus, all the code of your web project will be stored in Git, it will always be of
high quality and
proven .
For example, if you propose to make changes to the official code of the PHP framework Laravel, then when changes are made, tests are automatically run that check the operation of the framework, taking into account the new code.
Earlier we talked about the process of deploying a web application. This is what we need Git for. From your local machine, you upload the web application code to Git, after which the application will automatically start deploying on the server.
Laravel Forge - server without hassle. For automatic deployment from Git,
Laravel Forge will help us.
Through Laravel Forge, you can create a virtual server in
DigitalOcean ,
Linode, or specify access to your own server. At the same time, absolutely all the necessary software for the Laravel PHP framework will be configured.
Laravel Forge automatically installs updates related to system security. Forge will also easily install a free
SSL certificate from
Let's Encrypt .
You can give the Laravel Forge service access to your Git repository and with each change in the code on the server its latest version will be automatically deployed.
Want 10 servers? - No problem, Laravel Forge can install a
load balancer, create 10 virtual servers, copy the code from Git to each server and run the project.
Do you think everything?No, together with
Envoyer, you can launch a new code into operation without stopping the service at all.
Although I personally do not use Envoyer, I just wrote a small script in the Laravel Forge panel that runs every time I deploy the code and provides a replacement on the fly, while still keeping a few copies of the old code on the server itself.
→
Link to scriptResults
We created a comfortable working environment, installed IDE, Composer, Laravel PHP framework, wrote the project code, ran tests, studied the Git version control system, sent the code there, connected the Laravel Forge service, if desired also connected the Laravel Envoyer project, server from our git repository.
We can say that Laravel has guided us on a literate path in web development. There is still a lot to be learned, but we have already done a lot of work and can start working as a team with other developers.
Main features of the PHP framework Laravel
And now let's take a look at the capabilities of the Laravel PHP framework itself: which web applications the given PHP framework allows us to create, how advanced it is technically, and why it is so popular all over the world.
After the release of
PHP7 compared to
PHP5 , the scripts have become faster and have begun to use much less RAM, and in conjunction with
Zend OPCache show excellent results. In particular, the Laravel Forge service adjusts Zend OPCache for maximum performance.
That is why, when it comes to the performance of a PHP framework, they always carry out testing without caching, working with databases or files, mainly making many calls to a normal PHP page. In this regard, this PHP framework is not significantly different from all the others, but when it comes to scalability, flexibility, universality of embedded caching mechanisms and development speed, it is then that Laravel shows all its flexibility and power.
Laravel itself is constantly being improved and follows current trends. Studying it, you will not lag behind the world of web development, the main thing is not to focus on any particular version of the framework, but to develop with it. To do this, it is also necessary to explore the innovations of Laravel.
Various conferences are held annually, which can also be viewed online.
I will try to describe the main features of Laravel, so that you can assess the scale:
- MVC (English Model View Controller - model-view-controller) PHP-framework is built on the basis of well-known and reliable components of symfony .
- The necessary modules for the framework are connected in the form of service provider packages. In Laravel version 5.5, it is enough just to install the package via omposer, and it will be immediately available without having to write anything in the code.
- The framework code is separate from the developer code, each component is easily extensible.
- Web project code, CSS, JS, HTML pages are divided into separate directories. The framework uses the wonderful Blade template engine that allows you to separate the layout from the PHP code. The templating engine itself is so simple that even a beginner HTML coder can easily master it.
- Convenient routing, validation of incoming parameters.
- Caching, work with file storages, work with various databases.
- Migrations for the database, you can change the structure of the database and roll back the changes.
- Task queues, task scheduler, console, work with SSH.
- The huge functionality of Eloquent ORM allows you to fully protect yourself from SQL Injection attacks, as well as load data from several tables (solving the N + 1 problem) or process data from the database in parts.
- Laravel Collections - we can say that these are PHP arrays, but with very advanced features that save a lot of time.
- Caching routing files, configuration files, templates. This speeds up the work of the framework.
- Sending notifications in various ways: mail, Slack , etc., you can add it yourself.
- WebSockets support for creating real interactive applications.
- Multi-language support: add any languages ​​easily, and the Laravel-lang package already contains many translations.
- The artisan command line interface that allows you to generate models, controllers, notifications, run tasks from a job queue, and more.
- Laravel Tinker is an optional package that allows you to work with project code from the command line.
- Great opportunities for testing a web project, including filling the database with test data.
- The framework even has its own website with a library of packages .
- Need full-text search? Please - Laravel Scout , you can use Algolia , Sphinx and other drivers.
Impressive, isn't it? And I did not describe even half the possibilities.Using Laravel, you can generate a registration and login system with one team and easily connect
OAuth authentication services thanks to
Laravel Socialite or even create your own using
Laravel Passport .
For those who do not know OAuth, this is an opportunity to enter the site through social networks.This is only a small part of what Laravel can do, and if a novice PHP developer explores its capabilities, even without delving into the core of the PHP framework itself, this will certainly raise his level of knowledge not only in Laravel development, but in web development in particular, including teamwork skills and an understanding of the principles for developing high-load projects.
On the main site PHP-framework Laravel not without reason there is the motto:
“Love the beautiful code? We, too. PHP framework for webmasters. ”
After all, the code of the PHP framework Laravel is not only beautiful, pleasantly readable, but also very well thought out, and a lot of people think of any change, which allows you to create professional web applications at the level of a master in their field.
Useful links:
Website PHP framework Laravel.Main news PHP framework and news about various packages.
Laravel in Russian and
Russian-speaking community Laravel in VK.I highly recommend the site
https://laracasts.com , where
Jeffrey Way in his video tutorials demonstrates the capabilities of Laravel clearly and without unnecessary words, also tells a lot of useful things. In 2 minutes a person has time to tell more and more accessible than many in an hour.
I also recommend the book
" Refactoring to Collections " , where
Adam Wathan tells in detail about the possibilities of Laravel Collections. I guarantee your code will change for the better.
I recommend to install in every web project on Laravel:
PS: This article turned out to be quite voluminous and without technical details, but its main task is to give novice developers some introductory knowledge, explain what Laravel is and what opportunities it gives, and also show that Laravel is not just a PHP framework, but a whole an ecosystem that is constantly evolving. This is the trend that PHP developers should pay attention to.
Many things may be incomprehensible for beginners, but do not despair. Any term quickly enough can be found on the net. I just tried to summarize the information in exactly the order in which the novice developer should perceive it.
If you liked the article, I will write more in detail about each stage in this article with technical details and code.