Get up to 80 % extra points for free! More info:

Lesson 2 - Symfony and IDE installation

In the last lesson, Introduction to Symfony framework for PHP, we introduced ourselves to the Symfony PHP framework.

In this lesson, we will look at installing Symfony as the basis of our application. We will perform this installation using the Composer tool, either separately using the command line or within the PhpStorm IDE, so that even novice users who have just learned OOP and want to try to use a PHP framework can start the development.

To be sure, I would like to remind you once again that you need to know object-oriented programming in PHP to work with the framework. If you do not master it enough, please read the local tutorial first :)

Necessary software

The first thing we'll need is to prepare the right software. Surely you know that for the development of web applications in PHP you need a web server and we will also need some database. For beginners, I recommend installing a complete package, such as XAMPP.

We will need PHP 7.1 and higher for Symfony 4 and PHP 7.2 and higher for Symfony 5.

Subsequently, for those who are used to writing in a text editor such as PSPad, I would recommend switching to some IDE, which will make its use easier when working with the framework. However, this is not a condition. Here, to demonstrate the installation, I will show the use of PhpStorm, one of the most widely used professional IDEs. You will see for yourself that it is beautifully easy with it.

Furthermore, for the installation itself, we will use perhaps the most common method of installation in PHP, namely the Composer tool. Symfony builds on both the installation itself and other extensions. Therefore, you also need to download and install it or leave it all on PhpStorm, which also has built-in resources for working with this tool. The choice is yours again ;)

Creating a new project

So let's start by creating a new project in Symfony. You can choose one of the following methods.

Option 1: Install using Composer

As I mentioned, Composer is a third-party tool for installing and managing project dependencies in PHP in general. Once you have Composer installed and ready, all you have to do is create a folder where you want to create the new project, such as hello-world, and run a specific command from the command line to arrange the entire Symfony installation and check the necessary dependencies.

If you don't know how to run a command prompt in a folder, do so by clicking anywhere in the folder in Windows Explorer, pressing Shift, then right-clicking and selecting "Open a command prompt window here" (or a PowerShell window).

So we will create a new project with a single command:

composer create-project symfony/website-skeleton hello-world

This command created the basic structure of the project in the hello-world/ folder, according to the Symphony framework of the website-skeleton web application. This serves as a good basis for a new project in this framework.

Installation may take longer.

Option 2: Install using PhpStorm

If you have decided to work in PhpStorm and you have everything installed, all that remains is to create the project itself. We will do this similarly to any other IDE, by clicking on the button to create a new project.

A wizard will appear in the newly opened window and we will select the Composer Project button as the project type.

Creating a PHP Symfony project - Symfony framework basics

The next steps are:

  1. Fill in the name of the project, eg hello-world, together with the path where it should be saved.
  2. We also set that we want to download Composer and the path to our PHP interpreter (it can also be detected automatically).
  3. Last but not least, we will choose what we will actually install using Composer. In our case, it will be Symfony, specifically the skeleton of the web application (called website-skeleton). This serves as a good basis for our new project in this framework.

This will create the first project in Symfony within the PhpStorm IDE using the Composer tool.

Installation may take longer.

Project launch

Once we have created a new project with Symfony installed, it's time to run it on our web server and see the result. But here again you have a choice.

Option 1: Start using the embedded server

The easiest option, which does not require any additional configuration, is to run it using the built-in web server in PHP. Just call the following command in the project folder:

php -S 127.0.0.1:8000 -t public

Within the installed website-skeleton template, a web server built in Symfony is also available in the project, which we start in the project folder with the following command:

php bin/console server:run

So we can choose which server to use.

In PhpStorm, a runtime configuration can be set for both.

Option 2: Start using Apache server

It is clear that we need to run the project under an Apache server, the server needs to be running and the project folder located somewhere where Apache has access. What is not so clear is that we will have to set up a few redirects before starting.

Routing

First of all, it is necessary to realise that the directory structure of the project is built in such a way that we can display the page only after entering the public/ folder, where the index.php file is located. But we want the resulting address to start with the name of the project (eg http://localhost/hello-world/) and continue with a specific nice URL. Therefore, we do not want any public URL here and in addition we want to route all URLs to the same index.php file.

An elegant way to do this within an Apache server is to set up routing using .htaccess files, in our case two. One will automatically "move" us to the public/ folder and therefore the root URL will only be http://localhost/hello-world/. The second arranges the correct routing to the index.php already within the public/ folder itself.

So we start by creating the first .htaccess in the project directory, into which we insert the following code:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteRule ^$ public/ [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} !^public/
        RewriteRule ^(.*)$ public/$1
</IfModule>

We will no longer create the second file ourselves, but we will install it using Composer by running the following command in the root folder of the project:

composer require symfony/apache-pack

Result

Whatever method you choose, we come to today's result. After entering the appropriate URL into a web browser, you should see a window similar to the one shown below. For the embedded server the URL will be http://127.0.0.1:8000/ and for Apache http://localhost/hello-world/:

Symfony framework launched - Symfony framework basics

Welcome page can differ on Symfony versions.

If you see this window, then you've done everything right and you're well on your way to learning Symfony and next lesson, where we'll be programming something.

If for some reason you do not see this page, I recommend that you honestly go through the instructions again or try another method of installation or launch. And if you really don't know what to do anymore, you can always write me in the comments below the article ;)


 

Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.

Download

By downloading the following file, you agree to the license terms

Downloaded 4x (14.55 MB)
Application includes source codes in language PHP

 

Previous article
Introduction to Symfony framework for PHP
All articles in this section
Symfony framework basics
Article has been written for you by Lishaak
Avatar
User rating:
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