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

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

bin/console doctrine:database:create

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

bin/console make:entity

Create a migration file

bin/console make:migration

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

Create table with information from migration file

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

composer require orm-fixtures --dev

It’s time to make a fixture

bin/console make:fixtures

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

Create 10 articles named foo1, foo2, …, foo10

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:

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

Time to make the view & controller

bin/console make:crud

Now run the localhost website:

bin/console server:run

We are done. Let’s enjoy the (pure, ugly) website :))

Finish source code can find here(remember to run composer install if you want to run local server from it) https://www.dropbox.com/s/23aic3pg9ymdv6j/basic_crud.zip?dl=0