Introduction
Symfony is a popular PHP framework.
A framework is a set of components and functions that can be reused multiple times.
For example, it contains Twig, which is a powerful templating engine, so the PHP code is separated from your HTML code.
This tutorial is split into multiple parts. In this one, we go over how to set up our Symfony project. We go over to learn more about controllers and routes. Afterward, we learn a bit about the .env
file and start working with the database. Securing our application is crucial so we also take a look at authenticating users.
Series index
- Setup (You are here)
- Routes, templates and controllers
- Working with the database (Not yet finished)
- Security (Not yet finished)
Prerequisites
Please note: Those prerequisites are needed at the time of writing. The specific version required, may change at any time.
- PHP version 7.1.3 or higher.
- A fair amount of PHP experience is recommended
- Composer.
- In this tutorial, we will assume that composer is installed globally and accessible via the command
composer
- Otherwise, you can replace the
composer
command withphp path/to/composer.phar
(Read more…)
- In this tutorial, we will assume that composer is installed globally and accessible via the command
- A (simple) code editor, I am using Visual Studio Code
This tutorial is tested on the following operating system:
- Ubuntu 18.04.2 LTS bionic with PHP 7.2.15
Step 1 – Create a new Symfony project
We will start by creating a simple project. This is based on the symfony/website-skeleton
. It provides an already configured Symfony application, so we don’t have to configure everything ourselves. The last argument passed is the folder to use. In our case, we use the name of this tutorial: a-basic-guide-to-php-symfony
.
$ composer create-project symfony/website-skeleton a-basic-guide-to-php-symfony
(Click to expand) Your output should be similar to this
Now we can cd
(change directory) into the a-basic-guide-to-php-symfony
folder. The rest of this tutorial will assume that we are in this directory.
When you are developing “real” applications, using Git as a version control system is a great idea. You can initialize a repository and commit the initial state with the following command:(Click to expand) How to start a git repository
Step 2 – Starting the development server
We do not need to configure a webserver during development. PHP and Symfony bring everything needed. PHP can be used to run a development server with the CLI option -S
. Symfony provides a wrapper, specialized for Symfony’s needs. With the file bin/console
Symfony provides a simple interface to run different commands.
$ php bin/console server:run
[OK] Server listening on http://127.0.0.1:8000
// Quit the server with CONTROL-C.
PHP 7.2.15-0ubuntu0.18.04.1 Development Server started at Fri Mar 8 22:23:14 2019
Listening on http://127.0.0.1:8000
Document root is /path/to/a-basic-guide-to-php-symfony/public
Press Ctrl-C to quit.
Please note, the address:port part ([0.0.0.0:8080]
) is optional and is the address to listen to.
To keep the development server running in the background, the terminal multiplexer tmux is a good option.
Assuming you have not used a custom address port, we can now visit http://localhost:8000/. There we are greeted by the “Welcome to Symfony 4.2.3” page containing a few information about the application. On the bottom we find the profiler, a tool that is useful for debugging later. For now, it can safely be minimized by clicking the small X
on the bottom right.
Step 3 – Exploring the file system
Before we start creating our first “real” content, we explore the directory structure of Symfony a bit. The directory structure is like this:
a-basic-guide-to-php-symfony
├── bin
├── config
├── public
├── src
├── templates
├── tests
├── translations
├── var
└── vendor
- In the
bin
folder there are files meant to be used from the console. Here is also ourbin/console
file used to start our development server - As the name indicates several configuration files for everything from security to routing (matching the requested URL to the PHP Code) are located in the
config
folder - In the
public
folder are files located that do not need to be generated dynamically. CSS and JS files may be located here. When you deploy a Symfony application thepublic
folder must be set as webroot. - In the
src
folder, which stands forsource
, all of our infamous PHP code is located. templates
keep the twig templates as mentioned earlier.- Symfony has a
translations
engine built in; the translation files are held in this directory. - The
var
and thevendor
folder can be safely ignored but shall not be deleted!
Having done that, we are set an ready to learn something about Routes, Templates and Controllers in the next part of this tutorial.
Conclusion
By now we have setup symfony and started the development server. This is essential in order to follow along with the next parts.
Reprint: https://community.hetzner.com/tutorials/a-basic-guide-to-php-symfony
Leave a Reply