Symfony 4 Tutorial

From this:

CREATE SYMFONY 4 PROJECT
(ref: https://symfony.com/doc/current/setup.html –> DONT USE THE INSTRUCTION ‘symfony/website-skeleton’. USE ‘symfony/skeleton’ FROM https://knpuniversity.com/screencast/symfony/setup#play INSTEAD)
1. CREATE A NEW DIR ‘symfony4’ IN Documents/works/ DIR
2. GO INTO IT AND USE ‘composer’ TO CREATE THE PROJECT SKELETON:

TEST IT VIA INTERNAL PHP SERVER:

OPEN VIA BROWSER: http://127.0.0.1:8000/ –> WORKS

CREATE A VIRTUALHOST
1. CREATE A NEW conf FILE

HERE IS THE CONTENT:

2. Enable the site

3. Register the site in /etc/hosts

HERE IS THE CONTENT:

RESTART THE APACHE2 SERVER: sudo systemctl restart apache2
TEST IT VIA BROWSER: http://symfony4test.test/ –> WORKS

ROUTE AND CONTROLLER (ref: https://knpuniversity.com/screencast/symfony/route-controller#play)
Modify /config/routes.yaml FILE TO UNCOMMENT THE CONTENT AND CHANGE THE CONTROLLER AND THE ACTION:

CREATE THE CONTROLLER ‘ArticleController’  FILE AND CLASS. ALSO CREATE ‘homepage’ FUNCTION IN /src/Controller/ArticleController.php:

REFRESH THE BROWSER: http://symfony4test.test/
IT’D SHOW ‘Hello, This is my first page!’ TEXT!

USE ANNOTATION ROUTE (PREFERRED) INSTEAD OF MODIFYING /config/routes.yaml
USE COMPOSER TO INSTALL IT ‘composer require annotations’

BECAUSE WE DONT NEED routes.yaml, COMMENT THE CONTENT BACK (IN /config/routes.yaml):

MODIFY AGAIN /src/Controller/ArticleController.php AND ADD THE ANNOTATION

REFRESH THE BROWSER: http://symfony4test.test/
IT’D SHOW ‘Hello, This is my first page with annotations!’ TEXT!

FANCY WILDCARD ANNOTATION ROUTE
FOR EXAMPLE ADD THIS NEW ROUTE IN /src/Controller/ArticleController.php:

WE SHOULD OPEN IT ON BROWSER LIKE THIS: http://symfony4test.test/news/why-asteroids-taste-like-bacon
BUT I GOT ‘Not found’ ERROR.
OK. I NEED TO RUN ‘composer require apache-pack’

REFRESH BROWSER! –> STILL NOT WORKING
I NEED TO CREATE .htaccess FILE in /public/ DIR (ref: https://knpuniversity.com/screencast/symfony/route-controller#play
AND THE .htaccess CONTENT GOT FROM https://raw.githubusercontent.com/symfony/recipes-contrib/master/symfony/apache-pack/1.0/public/.htaccess):
/public/.htaccess

REFRESH BROWSER: http://symfony4test.test/news/why-asteroids-taste-like-bacon –> WORKS
IT’D PRINT ‘Future page to show one space article!’

USE ANNOTATION WITH PARAMETER
MODIFY /src/Controller/ArticleController.php:

REFRESH BROWSER: http://symfony4test.test/pages/test-page –> WORKS
IT’D PRINT ‘Here is the page “test-page” content!’

Leave a Reply

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