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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
teddy@teddy:~/Documents/works/symfony4$ composer create-project symfony/skeleton symfony4test Installing symfony/skeleton (v4.0.5) - Installing symfony/skeleton (v4.0.5): Downloading (100%) Created project in symfony4test Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 21 installs, 0 updates, 0 removals - Installing symfony/flex (v1.0.67): Loading from cache - Installing symfony/polyfill-mbstring (v1.6.0): Loading from cache - Installing symfony/console (v4.0.4): Loading from cache - Installing symfony/routing (v4.0.4): Loading from cache - Installing symfony/http-foundation (v4.0.4): Loading from cache - Installing symfony/yaml (v4.0.4): Loading from cache - Installing symfony/framework-bundle (v4.0.4): Loading from cache - Installing symfony/http-kernel (v4.0.4): Loading from cache - Installing symfony/event-dispatcher (v4.0.4): Loading from cache - Installing psr/log (1.0.2): Loading from cache - Installing symfony/debug (v4.0.4): Loading from cache - Installing symfony/finder (v4.0.4): Loading from cache - Installing symfony/filesystem (v4.0.4): Loading from cache - Installing psr/container (1.0.0): Loading from cache - Installing symfony/dependency-injection (v4.0.4): Loading from cache - Installing symfony/config (v4.0.4): Loading from cache - Installing psr/simple-cache (1.0.0): Loading from cache - Installing psr/cache (1.0.1): Loading from cache - Installing symfony/cache (v4.0.4): Loading from cache - Installing symfony/dotenv (v4.0.4): Loading from cache Writing lock file Generating autoload files Symfony operations: 4 recipes (e6e4209abb735359cca631db8c4b90fe) - Configuring symfony/flex (>=1.0): From github.com/symfony/recipes:master - Configuring symfony/framework-bundle (>=3.3): From github.com/symfony/recipes:master - Configuring symfony/console (>=3.3): From github.com/symfony/recipes:master - Configuring symfony/routing (>=4.0): From github.com/symfony/recipes:master Executing script cache:clear [OK] Executing script assets:install --symlink --relative public [OK] Some files may have been created or updated to configure your new packages. Please review, edit and commit them: these files are yours. What's next? * Run your application: 1. Change to the project directory 2. Execute the php -S 127.0.0.1:8000 -t public command; 3. Browse to the http://localhost:8000/ URL. Quit the server with CTRL-C. Run composer require server --dev for a better web server. * Read the documentation at https://symfony.com/doc |
TEST IT VIA INTERNAL PHP SERVER:
|
1 2 |
teddy@teddy:~/Documents/works/symfony4$ cd symfony4test/ teddy@teddy:~/Documents/works/symfony4/symfony4test$ php -S 127.0.0.1:8000 -t public |
OPEN VIA BROWSER: http://127.0.0.1:8000/ –> WORKS
CREATE A VIRTUALHOST
1. CREATE A NEW conf FILE
|
1 |
teddy@teddy:~/Documents/works/symfony4$ sudo gedit /etc/apache2/sites-available/symfony4test.test.conf |
HERE IS THE CONTENT:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<VirtualHost symfony4test.test:80> ServerName symfony4test.test ServerAlias www.symfony4test.test DocumentRoot "/home/teddy/Documents/works/symfony4/symfony4test/public" <Directory "/home/teddy/Documents/works/symfony4/symfony4test/public"> AllowOverride All Allow from All Require all granted </Directory> ErrorLog /var/log/apache2/symfony4test_error.log CustomLog /var/log/apache2/symfony4test_access.log combined </VirtualHost> |
2. Enable the site
|
1 |
teddy@teddy:~/Documents/works/symfony4$ sudo a2ensite symfony4test.test |
3. Register the site in /etc/hosts
|
1 |
teddy@teddy:~/Documents/works/symfony4$ sudo gedit /etc/hosts |
HERE IS THE CONTENT:
|
1 2 3 |
... 127.0.0.1 symfony4test.test ... |
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:
|
1 2 3 |
index: path: / controller: App\Controller\ArticleController::homepage |
CREATE THE CONTROLLER ‘ArticleController’Â FILE AND CLASS. ALSO CREATE ‘homepage’ FUNCTION INÂ /src/Controller/ArticleController.php:
|
1 2 3 4 5 6 7 8 9 10 11 |
<?php namespace App\Controller; use Symfony\Component\HttpFoundation\Response; class ArticleController { public function homepage(){ return new Response("Hello, This is my first page!"); } } |
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’
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
teddy@teddy:~/Documents/works/symfony4/symfony4test$ composer require annotations Using version ^5.1 for sensio/framework-extra-bundle ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 7 installs, 0 updates, 0 removals - Installing doctrine/lexer (v1.0.1): Loading from cache - Installing doctrine/inflector (v1.3.0): Loading from cache - Installing doctrine/collections (v1.5.0): Loading from cache - Installing doctrine/cache (v1.7.1): Loading from cache - Installing doctrine/annotations (v1.6.0): Loading from cache - Installing doctrine/common (v2.8.1): Loading from cache - Installing sensio/framework-extra-bundle (v5.1.4): Loading from cache Writing lock file Generating autoload files Symfony operations: 2 recipes (1eee9f4a26353ed781b272153022870a) - Configuring doctrine/annotations (>=1.0): From github.com/symfony/recipes:master - Configuring sensio/framework-extra-bundle (>=4.0): From github.com/symfony/recipes:master Executing script cache:clear [OK] Executing script assets:install --symlink --relative public [OK] Some files may have been created or updated to configure your new packages. Please review, edit and commit them: these files are yours. |
BECAUSE WE DONT NEED routes.yaml, COMMENT THE CONTENT BACK (IN /config/routes.yaml):
|
1 2 3 |
#index: # path: / # controller: App\Controller\ArticleController::homepage |
MODIFY AGAIN /src/Controller/ArticleController.php AND ADD THE ANNOTATION
|
1 2 3 4 5 6 7 8 9 10 11 12 |
... use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\HttpFoundation\Response; class ArticleController { /** * @Route("/") */ public function homepage(){ return new Response("Hello, This is my first page with annotations!"); } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
... class ArticleController { ... /** * @Route("/news/why-asteroids-taste-like-bacon") */ public function show() { return new Response('Future page to show one space article!'); } } |
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’
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
teddy@teddy:~/Documents/works/symfony4/symfony4test$ composer require symfony/apache-pack Using version ^1.0 for symfony/apache-pack ./composer.json has been updated Loading composer repositories with package information Updating dependencies (including require-dev) Package operations: 1 install, 0 updates, 0 removals - Installing symfony/apache-pack (v1.0.1): Downloading (100%) Writing lock file Generating autoload files Symfony operations: 1 recipe (8cd75672403cdc5a8053943729bbe843) - WARNING symfony/apache-pack (>=1.0): From github.com/symfony/recipes-contrib:master The recipe for this package comes from the "contrib" repository, which is open to community contributions. Do you want to execute this recipe? [y] Yes [n] No [a] Yes for all packages, only for the current installation session [p] Yes permanently, never ask again for this project (defaults to n): Executing script cache:clear [OK] Executing script assets:install --symlink --relative public [OK] |
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# Use the front controller as index file. It serves as a fallback solution when # every other rewrite/redirect fails (e.g. in an aliased environment without # mod_rewrite). Additionally, this reduces the matching process for the # start page (path "/") because otherwise Apache will apply the rewriting rules # to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl). DirectoryIndex index.php # By default, Apache does not evaluate symbolic links if you did not enable this # feature in your server configuration. Uncomment the following line if you # install assets as symlinks or if you experience problems related to symlinks # when compiling LESS/Sass/CoffeScript assets. # Options FollowSymlinks # Disabling MultiViews prevents unwanted negotiation, e.g. "/index" should not resolve # to the front controller "/index.php" but be rewritten to "/index.php/index". <IfModule mod_negotiation.c> Options -MultiViews </IfModule> <IfModule mod_rewrite.c> RewriteEngine On # Determine the RewriteBase automatically and set it as environment variable. # If you are using Apache aliases to do mass virtual hosting or installed the # project in a subdirectory, the base path will be prepended to allow proper # resolution of the index.php file and to redirect to the correct URI. It will # work in environments without path prefix as well, providing a safe, one-size # fits all solution. But as you do not need it in this case, you can comment # the following 2 lines to eliminate the overhead. RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$ RewriteRule ^(.*) - [E=BASE:%1] # Sets the HTTP_AUTHORIZATION header removed by Apache RewriteCond %{HTTP:Authorization} . RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # Redirect to URI without front controller to prevent duplicate content # (with and without `/index.php`). Only do this redirect on the initial # rewrite by Apache and not on subsequent cycles. Otherwise we would get an # endless redirect loop (request -> rewrite to front controller -> # redirect -> request -> ...). # So in case you get a "too many redirects" error or you always get redirected # to the start page because your Apache does not expose the REDIRECT_STATUS # environment variable, you have 2 choices: # - disable this feature by commenting the following 2 lines or # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the # following RewriteCond (best solution) RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L] # If the requested filename exists, simply serve it. # We only want to let Apache serve files and not directories. RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ - [L] # Rewrite all other queries to the front controller. RewriteRule ^ %{ENV:BASE}/index.php [L] </IfModule> <IfModule !mod_rewrite.c> <IfModule mod_alias.c> # When mod_rewrite is not available, we instruct a temporary redirect of # the start page to the front controller explicitly so that the website # and the generated links can still be used. RedirectMatch 307 ^/$ /index.php/ # RedirectTemp cannot be used instead </IfModule> </IfModule> |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
... class ArticleController { ... /** * @Route("/pages/{slug}") */ public function page($slug) { return new Response(sprintf('Here is the page "%s" content!', $slug)); } } |
REFRESH BROWSER: http://symfony4test.test/pages/test-page –> WORKS
IT’D PRINT ‘Here is the page “test-page” content!’