Simple application build with PHP Framework Symfony

Tram Ho

Last week I just joined the ec-cube project which is a cms build on symfony framework, in this article I will guide you to build basic CRUD application.


Symfony is a set of PHP Components, a Web Application framework, a Philosophy, and a Community – all working together in harmony.

  • Symfony framework: A leading PHP framework for creating websites and web applications. Built on the Symfony Components.
  • Symfony component: A set of separate and reusable components, some built-in PHP applications like Drupal, phpBB, eZ Publish …
  • Community: A community of more than 600,000 developers from over 120 countries.
  • Philosophy: Promote the professionalism, best practices, standardization and interoperability of applications.


  • Run up the command line wget -O - | bash
  • Install it on global: mv /home/linhdn1198/.symfony/bin/symfony /usr/local/bin/symfony
  • Check the version
  • Create new project symfony new my_project_name or composer create-project symfony/website-skeleton my_project_name


Install some components

  • composer require annotations : Allows you to use comments to define a route Example:
  • composer require twig : The template allows you to use some specific syntax beyond the view.
  • composer require symfony/maker-bundle --dev : Allows you to execute the command make command to create entity, controller, form …
  • composer require symfony/orm-pack : Supports database manipulation.
  • composer require form validator security-csrf : Support to create forms, validate data and secure csrf for forms.

Components in symfony

  • Entity: This class uses Doctrine to create entities and communicate with the database.
    • @ORMEntity(repositoryClass="AppRepositorySimNumberRepository") : Define Repository class for Entity.
    • @ORMId() Set $ ​​id as the primary key.
    • @ORMGeneratedValue() Set the self- @ORMGeneratedValue() value.
    • @ORMColumn(type="integer") Set data types.
    • There are also relations: @OneToMany , @ManyToOne , @ManyToMany

  • Repository: The class that ServiceEntityRepository.php in this class inherits EntityRepository.php that defines the basic functions find (), findOneBy (), findAll (), findBy ().

  • Controller: In controller can route @Route("<url>", name="<name_route>", methods={"<GET, POST...>"}) routes @Route("<url>", name="<name_route>", methods={"<GET, POST...>"}) .

  • Form: This class to create the form with the add method (‘<name_input>’, ‘<type_input>’, [<opstions>])

  • Twig: A template that allows to write short, easy to read and friendly syntax. Some basic syntax.
    • {{ ... }} : Used to display the contents of a variable or the result of a conditional expression.
    • {% ... %} : Used to handle some conditional logic, loop.
    • {# ... #} : Used for commenting.

Construction begins

  • Start the symfony server under local symfony server:start
  • Edit file .env DATABASE_URL=mysql://db_user: [email protected] :3306/db_name?serverVersion=5.7
  • My configuration is DATABASE_URL=mysql://root: [email protected] :3306/symfony?serverVersion=5.7
  • Create entity with the following command php bin/console make:entity SimNumber

  • After creating the entity, go to phpmyadmin to create the databse with the config name in the .env file and start the database gen.
    • php bin/console doctrine:schema:update --dump-sql Debug see command to create database
    • php bin/console doctrine:schema:update --force Update the database first
  • Now that we have the database, we use the cmd make crud, it will give us all the files and the source code for the crud function.

  • Here I customize the url / sim / number -> / sim-number in the src / Controller / SimNumberController.php line 13-> 15

  • To make the interface easier to see, I have added bootstrap to the templates / base.html.twig file and slightly modified the interface.


Share the news now

Source : Viblo