0% found this document useful (0 votes)
46 views

4 - PHP MVC Frameworks Introducing Symfony Lab

This document outlines the steps to create a basic CRUD website with Symfony 4.2: download Symfony CLI, create a new project, modify the DATABASE_URL to connect to a MySQL database, generate an Article entity, make a migration file to create the database table, insert mock data using fixtures, generate CRUD files to view and manage Article records, and run the local server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

4 - PHP MVC Frameworks Introducing Symfony Lab

This document outlines the steps to create a basic CRUD website with Symfony 4.2: download Symfony CLI, create a new project, modify the DATABASE_URL to connect to a MySQL database, generate an Article entity, make a migration file to create the database table, insert mock data using fixtures, generate CRUD files to view and manage Article records, and run the local server.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Create a basic CRUD website with

Symfony 4.2 and some command line


helpers
First download symfony CLI from https://symfony.com/download
After that you can use the “symfony” command to create project
Create new symfony project:

symfony new --full --version=4.2 basic_crud

Go to the project folder

cd basic_crud

Modify the DATABASE_URL environment variable in the .env file connect with your database,


for example if you use localhost mysql with username root, password empty, database
name basic_crud_db

DATABASE_URL=mysql://root:@127.0.0.1:3306/basic_crud_db?
serverVersion=mariadb-10.4.11

You can create the database directly in your database client interface, or create by Symfony
command line:

php bin/console doctrine:database:create

PROJECT WEB - WEBG301 1


Now let’s make simple article entity (with the “name” property)

php bin/console make:entity

Create a migration file

bin/console make:migration

PROJECT WEB - WEBG301 2


The make:migration will create an instruction file contain SQL query

In order to create the table, run following command

php bin/console doctrine:migrations:migrate

Now new table “article” already been created in our basic_crud_db database


Let’s insert some mock data (via coding)
First install fixture which help to insert database record by symfony code

PROJECT WEB - WEBG301 3


composer require orm-fixtures --dev

It’s time to make a fixture

php bin/console make:fixtures

PROJECT WEB - WEBG301 4


Now open the fixture file at /src/DataFixtures/ArticleFixture.php and add some code to create 10
articles

Run the file to insert records into database

bin/console doctrine:fixtures:load

Now 10 article records already inserted into database, we can check it by the command:

PROJECT WEB - WEBG301 5


php bin/console doctrine:query:sql "select * from article"

Time to make the view & controller

php bin/console make:crud

PROJECT WEB - WEBG301 6


Now run the localhost website:
php bin/console server:run

We are done

PROJECT WEB - WEBG301 7


PROJECT WEB - WEBG301 8

You might also like