C# week
Save up to 80 % on our C# e-learning courses. Only this week!
Get up to 80 % extra points for free! More info

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 Doctrine, Composer, Codeception or even content management systems such as Drupal, Joomla or 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.

MVC

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:

  • management,
  • logic,
  • output.

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.

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 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.

Life cycle

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:

obchod.cz/book/harry-potter

The page lifecycle is illustrated by the following diagram. We will describe it immediately.

Application life cycle in Symfony
  1. The request gets to the routing first. Here, according to the address, it is determined that we want something with books, so the BookController is called and the rest of the URL is passed to it.
  2. BookController will look at the parameters and finds that the user wants to list books called "harry potter". So the Controller will get the BookModel and tell him, that he wants these books. BookController performs 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.
  3. BookModel gets the name of the books in the parameter, which it retrieves from the database and returns.
  4. BookController obtains data from the model and passes this data to the view (template).
  5. 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.
  6. BookController gets back from the resulting HTML template and sends it to the user in response.
  7. 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 bundles. Bundle is a plugin representing a given component for a certain functionality (e.g. FrameworkBundle, TwigBundle, DebugBundle, etc.)
  • 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 :)


 

All articles in this section
Symfony framework basics
Article has been written for you by Lishaak
Avatar
Do you like this article?
No one has rated this quite yet, be the first one!
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 (1)

 

 

Comments

To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!