Web Application Fundamentals
Hello guys, Pablo here. Welcome to this lecture, introduction to Symfony.
We're going to cover today some basic concepts regarding the development of web applications using Symphony. Let's begin by the very basics: what are web applications?
Web apps, it's basically software that works over the internet to which you access using a web browser. You have two sides. There's the client side, which is executed on the user's computer. The languages used on that side are HTML, CSS, JavaScript. It's used to show the user what's happening on the web application and to get the user input. On the server side, there are multiple languages you can use. Throughout this course, we'll be using PHP and the Symfony framework. For database engine, we'll be using MySQL.
What is the Symfony Framework?
Now, what is Symphony?
Symphony is a PHP framework and it's a toolbox. It is not only that, but also it's a methodology, a way of creating your application so that it's organized, stable, scalable. And Symphony is also a community of developers who've created this open-source framework and also who work on extensions and plugins for this framework.
The benefits of using Symphony is that it enforces best practices. It basically forces you to use a certain way of working, to put your files in certain folders. It's all flexible that you can change as well, but if you don't go into changing it, you're forced to use a certain folder structure that makes the code very organized. It's easy for other people who need to collaborate with you to know where each thing is so that you don't have to worry about creating tools, but you worry instead of just making your web app. The code is easier to maintain and it's all modular, up to the point where the actual Symphony framework is a plug-in within a Symfony application.
The Symfony Request-Response Lifecycle
Some basic HTTP concepts that are the core concept on how Symfony works: the client side makes a request to the server. The server does processing, database queries, and it sends back a response. That's the most important thing in web applications, and it's ridiculously simple, but you are not going to believe how often developers forget about this and don't think with this model in mind when they're making web applications.
Um, the usual flow when it comes to Symphony is firstly, there's a request from the client and it has a domain name and it will have some URL afterwards. This URL part are called routes in Symphony. The front controller, app.php, will get this request. It'll be sent down to the kernel, which is the heart of the Symfony application. In the kernel, libraries are loaded and a few other configuration-related things happen. But your route, in this case hello-world, will be taken care of by the routing engine. If that route is found, it will point to a certain controller file. And in Symphony, actions are basically methods inside a controller that you can point to using routes.
Always, in every single of these cycles, the action has to return a response object. And that will carry out the response that will eventually be sent back to the client. And the response can be HTML, JSON, XML, image files, anything you can think of. But this is the way it flows.
Core Concepts: Bundles, Environments, and Services
Some more concepts. In Symphony, everything, everything is a bundle. Bundles are the way, that's how plugins are called: modular pieces of code that that that have their own functionality. So all the code that you create will be inside of bundles. Third-party libraries are put in bundles as well. Even the actual Symphony library is treated as a bundle. There's templates where the views are stored to show the user. Those views can be PHP views or they can use a language called Twig, which is a templating language.
You can work in different environments within Symphony, and that has to do with loading different configuration files. Two of these environments are development and production. There's also testing environment and you can create your own environment too. When you're developing, you'll usually use the development environment as it will have configuration that will make development easier. The cache will be rebuilt each time, so you're always going to see the latest things and you're also going to have like a debug toolbar that can help you out with some things.
There's a concept of services. Services are libraries that you can inject within your code so that they're not always loaded. You sort of inject them, that's called dependency injection. You inject them when you need to use them. And Symphony uses, creates cache as well, so that in production environment, the loading of some things is a lot faster.
Templating with Twig
This is just an example of what Twig looks like, the templating engine that I was, templating language that I was talking about. So, see, this is how you will sort of echo a variable and that's how you can iterate. And the advantage of using Twig is that it's a lot cleaner than PHP when you're using PHP templates, but it's really up to you which one you want to use. I use, I prefer using Twig.
Database Management with Doctrine ORM
Databases are treated here, we'll treat them, we'll use an ORM, which is a library, in this case called Doctrine, that will treat databases as if it's object-oriented. It's optional, you can use another ORM if you want or you cannot use one at all. I prefer using it because I find it easier to maintain, easier to work in general, and especially when you're working with other developers, it's a lot easier when you're using an ORM. And Doctrine also allows you to create and update your database using command line tools, which is quite powerful.
Configuring Your Application
Configuration files in Symphony can be in different file formats. Most of the core ones are going to be in a format called YML. And then for things such as routing and some Doctrine configurations and some constraints as well, you get to choose if you want to use something called annotations, which are things that you write in the comments area in the PHP file. You can use that or YML, XML, or just PHP classes.
Throughout this course, we'll be using YML for the main configuration files and then when we get to choose, we'll use, we'll choose annotations as it creates less files, so you don't have to be changing through different files that much. But it's really up to you again. So that's one of the things about Symphony that it's very flexible and you can adapt it to the way that fits better with your own workflow.
Resources for Learning Symfony
Now, where to get help?
Symphony.com, the official Symphony website. It has two awesome ebooks. One is called 'The Book', which covers all the basics, and then 'The Cookbook', which has, is like a recipe book, has different tips, things you can do with Symphony. There's a Google group that you can write for support if you need, if you have questions, if you're running into issues, and they'll help within, usually within minutes you'll get an answer. And there's also an official forum as well. So that's those are the basic concepts that you need to keep in mind before even looking at the code as it will make what comes next a lot easier.