Get Started with Symfony 4

Mouna Ben Hmida
3 min readJan 23, 2018

--

For beginners ..

Directory structure differences

  • app/AppKernel .php -> src/Kernel.php
  • No AppBundle
  • No app.php and app_dev.php
  • app/config/parameters.yml -> .env
  • app/Resources/views -> templates
  • Empty project size : 9MB
taken from here

Let’s build the first app

1) Prepare your work-space

  • choice your best text editor

about me I use visual code and I install the framework snippets to make the tasks easier + integrated terminal
As well you can use phpStorm as IDE .

2) Install composer:

( if you have composer already installed , skip this step )

  • Install dependencies
sudo apt-get updatesudo apt-get install curl php-cli php-mbstring git unzip
  • Download and install composer
cd ~curl -sS https://getcomposer.org/installer -o composer-setup.php
  • Install composer globally
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

3) Create a project :

composer create-project symfony/skeleton your_project
cd your_project

4) Install dependencies

To have the application works , it requires at least TwigBundle and DoctrineBundle
You can install them one by one :

composer req twig
composer req doctrine

Or you can install EasyAdminBundle . EasyAdminBundle is a back-office manager . It will install its dependencies by itself which TwigBundle and DoctrineBundle. are one of them .

  • Install EasyAdminBundle ❤
composer require admin 

You can notice how much Symfony flex is powerful . Before Symfony flex , coders have to follow 4 steps to have EasyAdminBundle installed and configuired (Download , Enable the Bundle , Load the Routes and Prepare the Web Assets )

  • Configure your Bundle ( config/package/easy_admin.yaml)
easy_admin:entities:#        # List the entity class name you want to manage#        - App\Entity\Product
# - App\Entity\Category
# - App\Entity\User
# Add your new entity here
App\Entity\Your_Entity

We will create the new entity in following step

5) Database :

  • Create an ORM entity (src/Entity)
<?php
namespace App\Entity ;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*
*/
class Your_Entity {/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
public $id ;/**
* @ORM\Column(type="string")
*/
public $description;
}
?>
  • Setup database (.env)

database name , user name , user password and port database port
(.env) , user name , user password are configured when installing MySQL on your machine . If your don’t have MySQL installed and configured follow this tutorial

DATABASE_URL=mysql://db_user:db_password@127.0.0.1:3306/db_name
  • Create database
php bin/console doctrine:database:create 
php bin/console doctrine:schema:create
  • Test database with easyAdmin (skip this step if you didn’t install easyAdmin)
php bin/console server:run

Then browse to http://127.0.0.1:8000/admin/

You should have something like that
  • Test database with MySQL command line :
mysql -u your_username -p
your_password
show columns from your_entity from your_bdname ;
You should have something like that

5) Serious work starts now

  • Create your first controller
  • Route your actions

Now , I will deal with annotations . Annotations make tasks easier .

With Symfony 4 you can replace this code ( config/routes.yaml)

home_page:
path: /home
controller: 'App\Controller\DefaultController::Home'

by annotation added in the controller behind the action

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;/**
* @Route("/home")
*/
public function HomeAction()
  • Create a view

It’s enough for today ❤

I’m Mouna , a developer from Tunisia . I love sharing knowledge and life experience .

--

--