Symfony 4 – Blog App

Ref: https://auth0.com/blog/symfony-tutorial-building-a-blog-part-1/
https://symfony.com/doc/current/setup.html

Install the latest Symfony 4 via Composer with the following command:

Update DotEnv File
In your root directory, there is a file called .env (usually this file is hidden. so set your system to show the hidden files), you should see something similar to the following:

Change to this:

run the following command

which will create a database with the value of your database name.
<!– START SKIP –>
IF YOU GOT ERROR LIKE MINE. IT’S BECAUSE MY PHP SETTING & APACHE2/NGINX SERVER DIDN’T WORK WELL. SKIP IT IF YOU DONT HAVE ANY ERROR LIKE THIS :

I NEED TO CHECK MY PHP VERSION. CLI ALREADY 7.2.

BUT THE SERVER STILL 7.0 (http://localhost/phpinfo.php). I ALREADY HAS 7.2 BUT SOME EXTENSIONS ARE MISSING. SO INSTALL THEM

TO ENABLE PHP 7.2 ON APACHE2 SERVER, PLS DO (MAKE SURE NGINX IS STOPPED IF IT’S RUNNING AND START APACHE2):

TEST: http://localhost/phpinfo.php

IF NEEDED TO CHANGE THE PHP SETTING, PLS EDIT:

TO ENABLE PHP 7.2 ON NGINX SERVER, PLS DO (MAKE SURE APACHE2 IS STOPPED IF IT’S RUNNING AND START NGINX):

THE NGINX MAIN CONFIG IN /etc/nginx/nginx.conf
THE DEFAULT VIRTUALHOST IN /etc/nginx/sites-available/default
EDIT IT TO ENABLE PARSING PHP FILE:

CHANGE THE CONTENT TO ENABLE PHP 7.2 FPM:

THEN CHANGE PHP7.2-FPM SETTING:

CHANGE THE CONTENT:

THEN RESTART PHP7.2-FPM SERVICE:

CHECK NGINX SYNTAX: sudo nginx -t
RESTART NGINX: sudo systemctl restart nginx
OPEN IT: http://localhost/phpinfo.php (NEED TO CLEAR THE BROWSER CACHE FIRST)
<!– END SKIP –>

OK. I FIXED THE CREATION DATABASE ERROR. RUN AGAIN

HERE IS THE WORKING RESULT:

Create the Blog Controller
Create new BlogController by running the following command

When it asks for The class name of the controller to create, type in: BlogController.

THEN CHANGE THE ROUTE IN src/Controller/BlogController.php
FROM:

TO:

THEN TEST IT VIA SYMFONY WEB SERVER. RUN:

OPEN IT ON YOUR BROWSER: http://127.0.0.1:8000
THEN you’ll be shown a Hello BlogController! page.

Creating a New Author Entity
Create new Author entity by running the following command:

OPEN A PHP FILE  src/Entity/Author.php, THEN REPLACE IT.
FROM:

TO:

Creating a New BlogPost Entity
Create new BlogPost entity by running the following command php

When it asks for The class name of the entity to create, type in: BlogPost.

Once the command has finished running, you’ll find a new file in src/Entity called BlogPost.php. Open this and configure it like so:

Although you have created these entities, your database still has no tables in there. Based off these entities, Doctrine can create the tables we’ve specified. In order to do this, all you have to do is run:

Install Doctrine-Fixtures
We want to just populate some data into the newly created tables as examples during the creation of the blog. So install doctrine-fixtures.

Create Author and BlogPost Fixtures
Create a new file (and the directories the file is stored in) under src/DataFixtures/ORM/Fixtures.php and insert the following into the file:

Running Fixtures
Let’s run the fixtures! php bin/console doctrine:fixtures:load

 

Leave a Reply

Your email address will not be published. Required fields are marked *