How to Install Symfony 6 (PHP Framework)
Coding (Symfony)
A quick guide to Install Symfony 6 with Composer and not
Johnny just finished reading the newspaper.
He read that a man had made a few millions dollar during the last year.
He found very cheap houses in the county and resold them at a higher price quickly.
Johnny is a beginner developer and has some money saved up.
So decided to create a program that checks online real estate portals to find the ones with the best price.
He only knows vanilla PHP at the moment and so starts doing everything from scratch.
A month passed by and he only finished the authentication and authorization functionality.
He realizes that the task he set for himself will take too long to be achieved with success and gave up.
He would have been much better off learning a PHP framework and letting it do all the hard lifting.
What is a PHP framework?
A PHP framework is a system that is used for creating web applications.
It includes plenty of components and libraries with built-in functionalities.
PHP Frameworks are very useful in that they already contain features out of the box.
You do not need an authentication system or a routing system.
Need a connection to a database? is already there.
This will save you and Johnny a lot of time as you can focus on the features that make your website unique instead.
Why use Symfony 6?
There are many frameworks out there.
Laravel, CodeIgniter, CakePHP, etc.
Another great one is Symfony, in specific Symfony 6.
Symfony 6 has many pros like its speed and the number of components available.
Also, its big community makes it one of the most troubleshot frameworks on the internet.
Requirements
It would be amazing to have Symfony up and running with no preparation right?
Unfortunately, some steps need to be ready before you can start cracking on with it.
You need to have the basics of PHP.
This seems so simple that looks actually stupid to suggest but it is the truth.
I have seen many people approaching Frameworks to have no clue about the main language.
The result is that when they need to change jobs or frameworks they cannot use helpers they thought were part of the language.
At the moment I am writing this post Symfony requires PHP 8.1 to work.
It also needs a few PHP extensions such as Ctype, iconv, PCRE, Session, SimpleXML, and Tokenizer.
Don’t worry as most often than not, they are automatically installed with PHP.
A tool that you need to be able to work with the framework is Composer.
You can read about it in my introduction to Composer series.
Lastly, it is advised to install the in-house CLI.
It supplies several tools that you need to run Symfony locally.
You can check whether you fulfill all your requirements using the command.
symfony check:requirement
Creating Symfony Applications
Symfony allows you to create different types of applications.
It is mainly used to build web applications.
You can use it to create APIs or console applications.
You can opt for one or the other by adding the webapp flag to the command.
Also, there are 2 different methods you can choose when creating a new application in Symfony.
The first one is by using the Symfony binary that I showed above.
The second is by using Composer directly.
Here are the commands to use when creating a web application.
#using composer composer create-project symfony/skeleton:"6.1.*" myProject cd myProject composer require webapp #or using the symfony binary symfony new myProject - version="6.1.*" --webapp
If you are creating a console application or an API you can avoid requiring webapp component or adding the flag.
#using composer composer create-project symfony/skeleton:"6.1.*" myProject #or using the symfony binary symfony new myProject --version="6.1.*"
All of these commands will create a myProject
directory and add the initial skeleton code to it.
Running Symfony Applications
There are many ways to run a PHP project on your computer.
You can use Docker to run it or set up an Nginx or Apache web server on your local machine.
The best way though is to run your project using the built-in web server provided by Symfony.
It is easy to start with and provides a security certificate, HTTP2 support, etc.
To start your project type the following command into your terminal.
cd myProject symfony server:start
If there will be no error message you can just visit your local host at port 8000 and get to the front end of your website.
to stop the server just press CTRL+C on the terminal.
Installing Packages
During the first sections of this post, I highlighted how important is to have a grasp of Composer.
The reason is that Symfony makes heavy use of packages.
When implementing packages into the framework, these often need to be updated and configured.
Sometimes extra files need to be added manually.
Luckily the binary provides a tool called Flex.
Flex simplifies the installation of those external packages by automating the process.
A clear example of it is the logger package.
if you were to only rely on Composer and require logger, the terminal would prompt an error message.
It would say the logger does not exist.
If Flex is enabled, it will pull the package and add other components required to make the logger work.
The reason this happens is that Symfony uses Recipes.
Recipes are instructions that install packages into your applications.
Those recipes are stored in a file that the framework creates called symfony.lock.
If your project is on a GIT repository, this file has to be committed too.
If you do not know how it works yet, here is a quick introduction to GIT.
Symfony LTS Versions
Symfony provides long-term support” (or LTS for short) versions.
These special versions are released every couple of years.
You can check the last one on the Symfony releases page.
If you want to create an application that uses the latest LTS you need to add the version flag on the command.
symfony new my_project_directory --version=lts
Conclusion
Symfony is an astonishing tool that can be used to create a variety of PHP projects.
From web applications to APIs and microservices.
This framework is very easy to start with and with all its components there is no limit to what you can do with it.
What is the next project you are going to build with it?
If you are interested in using it in the future subscribe to the newsletter.
I am writing a series of articles that will go deeper into it and its best usage.