Skip to content
Sep 11

1. Set-up the Symfony project

1. Set-up the Symfony project

Soon I will publish an YouTube tutorial from this Symfony 3 tutorial

Symfony 3 is a framework what makes developing easy. All the components are modular, so we can easily use and extend it. One of the nice features is the Symfony CLI. To create a new project, we can use this tool in the terminal.

First we need to install Symfony CLI. To do this simply execute in a terminal the following commands. It will create a command in the bin folder and make it executable.

sudo curl -LsS https://symfony.com/installer -o /usr/local/bin/symfony
sudo chmod a+x /usr/local/bin/symfony

Set-up the project

To start creating a project, we can use the Symfony CLI. With the command we can create a new software project. In your case, you can change DevShop by any name you like. It will create a project containing the project root and different folders. Composer file will be set-up, so you can later install packages and create symlinks for the public files (css, js and images)

symfony new DevShop

This can take a while, when it downloads the source code. You will see the progress.

Downloading Symfony...

   5.8 MiB/5.8 MiB ▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓  100%

Preparing project...

  Symfony 3.3.9 was successfully installed. Now you can:

   * Change your current directory to /home/michael/gitlab/test/DevShop

   * Configure your application in app/config/parameters.yml file.

   * Run your application:
       1. Execute the php bin/console server:start command.
       2. Browse to the http://localhost:8000 URL.

   * Read the documentation at http://symfony.com/doc

Run the project

After finishing the new project. You get a small summary with information about the project. If you go to the project folder. You can execute the command:

php bin/console server:start

to start the internal server. Of course this it not production ready. Only for development purpose.  The’re different ways to reach the website. First of all the development environment:

http://localhost:8000 or the production environment:

http://localhost:8000/app.php

If you want create an production environment, you can best install Apache2 or Nginx to create Virtual Hosts. With mod_rewrite(Apache2) or Rewrite(Nginx) modules, can be beautiful urls created.

Use Apache2 to create a permanent server

To make development more easier, without need to start the server every day. We create a virtual host, pointing to our development directory. After set the configuration, we can restart the Apache2 server and use the development server right away.

<VirtualHost *:80>
    ServerName shop.dev
    ServerAlias www.shop.dev

    DocumentRoot /var/www/DevShop/web
    <Directory /var/www/DevShop/web>
        AllowOverride None
        Order Allow,Deny
        Allow from All

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app.php [QSA,L]
        </IfModule>
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/project>
    #     Options FollowSymlinks
    # </Directory>

    # optionally disable the RewriteEngine for the asset directories
    # which will allow apache to simply reply with a 404 when files are
    # not found instead of passing the request into the full symfony stack
    <Directory /var/www/project/web/bundles>
        <IfModule mod_rewrite.c>
            RewriteEngine Off
        </IfModule>
    </Directory>
    ErrorLog /var/log/apache2/project_error.log
    CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>

You should create a /etc/hosts entry for the virtual host

127.0.0.1          www.shop.dev shop.dev

Now you’re ready to create a full functional web shop.

The Symfony toolbar

Symfony has default in the development environment the possibility to use a own toolbar. The toolbar contains many informing tools what we can use. First of all in the left, it shows the status code for the current page. Further it shows the current URL name, response time, memory used to generate the page, resources loaded and how fast, security info and twig template parse information.

Toolbar

URL name
Every URL in Symfony routing table, contains a name. The name is easier to remember then the exact path. Also PhpStorm can autocomplete paths by name. When you decide to change the URL, you should only change it in the annotation. This makes changes a lot easier. Later more about this topic.

<?php

/**
 * @Route("/", name="homepage")
 * @Method({"GET"})
 */
public function indexAction()
{
    return $this->render('@App/default/index.html.twig');
}

Performance

At the end performance of the page is very important. Nobody wants wait for a webpage to load. After a few seconds you will loose your possible customer. So the performance information is important.
If you click on the performance link in the toolbar, you will get the profiler, where you can see information about the performance of the page. It will give you information about the controllers, events but also Doctrine and template parsing.

Memory usage
Next you will see the memory usage to create the page. It is easily to see if you have a problem with memory leakage. If the number become out of range, you can directly act on it.

Call information
In the view of the call information, you will see more information about what is happening when the call is executed. Caching information is here available. What is cached, what can be improved. Keep an eye on it.

Security
At the end, if we want let the users login. Then it is during development important that we know what roles the user have. As who we are logged in etc. That is where the security feature from Symfony comes look around the corner. Also give Symfony the opportunity to login as a different user. Later more about it. It will make your life easier!

Template rendering
When you use Twig for template parsing, Symfony toolbar and profiler will give more information about this. It will give more information about performance, but also how much time a template is parsed. When by example, you will use a template to render a list of products, it will give more information about it.

Ajax calls
Under water you can use Ajax, normally you not get all the information about it. But the toolbar give more information about the Ajax calls executed. It will give a status code and you can directly click on the profiler page of the call executed.

Symfony profiler

At this point we already have handled much of the possibilities of the toolbar. But the real power is in the Profiler. For every call there is a lot of information available. Performance information, but also Form, Debug, Logs, Exceptions, Emails and a lot more!

Profiler Tool

Just click around to see all the information or look soon my extended video about it.

Thanks for taking time to read my tutorial. Soon I will publish my second article and the video about this tutorial. If you want know more about this subject, just write me a message!

Intro Symfony3 Tutorials