Lesson 1 - Introduction to Symfony framework for PHP
Welcome to the first lesson of the online course about creating web
applications in the PHP framework Symfony. Symfony is a complete framework for
PHP, consisting of a set of PHP components that greatly simplifies the creation
of web applications. The framework is well-designed object-oriented and is very
widespread worldwide. Large and small projects operate on it, you can look at
their list and it is also worth
mentioning that some of its components use other tools, such as
Codeception or even
content management systems such as
PrestaShop. If you know it, you will always find a good job.
In today's lesson, we'll explain why we should generally use PHP frameworks and talk about how Symfony works.
Why use it?
The framework is a comprehensive set of matching libraries. In PHP, you can't do without quality libraries for more serious projects. 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 find that you've reprogrammed a database class, form handler, or table paging in each project over and over again, you'll need to think of creating simple components for these basic activities.
- From practical experience, I can tell you that if you program in pure PHP, then about 50% of your code is unnecessary ballast. Using quality components, 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 still invent the wheel and solve the same routine issues (such as how to verify whether the user is an administrator), on the contrary, you will devote yourself to interesting functions of the application and that's what programming is all about.
You can create your own framework or use one ready-made, which is Symfony.
What you need to use Symfony
As mentioned in the introduction, Symfony is mainly a large set of reusable components on which the framework for creating web applications is built. When you program in Symfony, you still program in PHP and, in addition, you use these components, which are object-designed and build on the principles of the MVC architecture. Therefore, to use Symfony, you must have advanced experience with PHP, know well object-oriented programming and at least the basics of MVC architecture. Learning Symfony without this knowledge is a waste of energy, because you will not understand its principles and you will only lose your time. If you are unfamiliar with OOP or MVC, please read these two local courses first.
How Symfony works
In simplicity, let's describe the basic technologies and terms that will appear in the series.
As already mentioned, Symfony is a classic MVC framework, so it's probably good to review what that actually means.
The application is based on parts of three types, which are divided into 3 basic tasks in the application:
Only such a divided application is clear and easy to expand.
- Controllers, control - The controller is the part with which the user communicates. The user passes the parameters to the controller and then it returns the HTML page. The controller typically passes parameters to the models from which it obtains data. It passes this data to views (templates), which incorporate the data into some HTML code. This HTML code is sent by the controller to the user in the browser. So it acts as such a mediator.
- Models, logic - Models contain logic of the application such as work with databases or calculations. Each data entity usually has its own model (user, article, comment,...).
- Views, output - Twig templates contain HTML code. Twig is a template system that allows you to insert data from PHP into HTML templates using special tags.
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 call the appropriate controller to handle his query. In Symfony, there are several ways to manage the routing of so-called "nice" URLs, most often annotations are used directly for individual methods of controllers.
When using the framework, you have to be sure of what is going on inside, otherwise you are degrading from a programmer to an experimenter, perhaps a magician, and you could certainly come up with a few other professions. Let's describe the application life cycle in Symfony - i.e. what happens when the user enters in the address bar, for example:
The page lifecycle is illustrated by the following diagram. We will describe it immediately.
- The request gets to the routing first. Here, according to the address, it is
determined that we want something with books, so the
BookControlleris called and the rest of the URL is passed to it.
BookControllerwill look at the parameters and finds that the user wants to list books called "harry potter". So the Controller will get the
BookModeland tell him, that he wants these books.
BookControllerperforms the appropriate method of the class according to the given URL, in this case displaying the list of books. However, it can also, for example, add or delete a book using other methods contained in it.
BookModelgets the name of the books in the parameter, which it retrieves from the database and returns.
BookControllerobtains data from the model and passes this data to the view (template).
- The template contains an HTML page for the book list and some Twig tags in it, into which this data is automatically added. The Twig engine automatically enters this data.
BookControllergets back from the resulting HTML template and sends it to the user in response.
- The user will see an HTML page in the browser and have no idea about everything we have described here.
Feel free to go through the diagram several times to be sure of the whole principle.
What else you should know
- Each controller typically has several of its templates for different methods (detail, addition, list, ...).
- Related models and controllers (with templates) are grouped into so-called
Bundleis a plugin representing a given component for a certain functionality (e.g.
- All functionality of the applications in Symfony comes from individual bundles (components), therefore our future applications will form a separate bundle with the above-described MVC architecture inside.
That would be all for an introduction.
In the next lesson, Symfony and IDE installation, we will create our first project in Symfony
No one has commented yet - be the first!