{"id":2695,"date":"2018-01-31T07:55:54","date_gmt":"2018-01-31T07:55:54","guid":{"rendered":"http:\/\/myprojects.advchaweb.com\/?p=2695"},"modified":"2018-01-31T09:47:37","modified_gmt":"2018-01-31T09:47:37","slug":"symfony-4-tutorial","status":"publish","type":"post","link":"https:\/\/myprojects.advchaweb.com\/index.php\/2018\/01\/31\/symfony-4-tutorial\/","title":{"rendered":"Symfony 4 Tutorial"},"content":{"rendered":"<p>From <a href=\"http:\/\/myprojects.advchaweb.com\/index.php\/2018\/01\/24\/ubuntu-16-04-3-installation-problem-and-solution\/\">this<\/a>:<\/p>\n<p>CREATE SYMFONY 4 PROJECT<br \/>\n(ref: https:\/\/symfony.com\/doc\/current\/setup.html &#8211;&gt; DONT USE THE INSTRUCTION &#8216;symfony\/website-skeleton&#8217;. USE &#8216;symfony\/skeleton&#8217; FROM https:\/\/knpuniversity.com\/screencast\/symfony\/setup#play INSTEAD)<br \/>\n1. CREATE A NEW DIR &#8216;symfony4&#8217; IN Documents\/works\/ DIR<br \/>\n2. GO INTO IT AND USE &#8216;composer&#8217; TO CREATE THE PROJECT SKELETON:<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy:~\/Documents\/works\/symfony4$ composer create-project symfony\/skeleton symfony4test\r\nInstalling symfony\/skeleton (v4.0.5)\r\n  - Installing symfony\/skeleton (v4.0.5): Downloading (100%)         \r\nCreated project in symfony4test\r\nLoading composer repositories with package information\r\nUpdating dependencies (including require-dev)\r\nPackage operations: 21 installs, 0 updates, 0 removals\r\n  - Installing symfony\/flex (v1.0.67): Loading from cache\r\n  - Installing symfony\/polyfill-mbstring (v1.6.0): Loading from cache\r\n  - Installing symfony\/console (v4.0.4): Loading from cache\r\n  - Installing symfony\/routing (v4.0.4): Loading from cache\r\n  - Installing symfony\/http-foundation (v4.0.4): Loading from cache\r\n  - Installing symfony\/yaml (v4.0.4): Loading from cache\r\n  - Installing symfony\/framework-bundle (v4.0.4): Loading from cache\r\n  - Installing symfony\/http-kernel (v4.0.4): Loading from cache\r\n  - Installing symfony\/event-dispatcher (v4.0.4): Loading from cache\r\n  - Installing psr\/log (1.0.2): Loading from cache\r\n  - Installing symfony\/debug (v4.0.4): Loading from cache\r\n  - Installing symfony\/finder (v4.0.4): Loading from cache\r\n  - Installing symfony\/filesystem (v4.0.4): Loading from cache\r\n  - Installing psr\/container (1.0.0): Loading from cache\r\n  - Installing symfony\/dependency-injection (v4.0.4): Loading from cache\r\n  - Installing symfony\/config (v4.0.4): Loading from cache\r\n  - Installing psr\/simple-cache (1.0.0): Loading from cache\r\n  - Installing psr\/cache (1.0.1): Loading from cache\r\n  - Installing symfony\/cache (v4.0.4): Loading from cache\r\n  - Installing symfony\/dotenv (v4.0.4): Loading from cache\r\nWriting lock file\r\nGenerating autoload files\r\nSymfony operations: 4 recipes (e6e4209abb735359cca631db8c4b90fe)\r\n  - Configuring symfony\/flex (&gt;=1.0): From github.com\/symfony\/recipes:master\r\n  - Configuring symfony\/framework-bundle (&gt;=3.3): From github.com\/symfony\/recipes:master\r\n  - Configuring symfony\/console (&gt;=3.3): From github.com\/symfony\/recipes:master\r\n  - Configuring symfony\/routing (&gt;=4.0): From github.com\/symfony\/recipes:master\r\nExecuting script cache:clear [OK]\r\nExecuting script assets:install --symlink --relative public [OK]\r\n\r\nSome files may have been created or updated to configure your new packages.\r\nPlease review, edit and commit them: these files are yours.\r\n\r\n              \r\n What's next? \r\n              \r\n\r\n  * Run your application:\r\n    1. Change to the project directory\r\n    2. Execute the php -S 127.0.0.1:8000 -t public command;\r\n    3. Browse to the http:\/\/localhost:8000\/ URL.\r\n\r\n       Quit the server with CTRL-C.\r\n       Run composer require server --dev for a better web server.\r\n\r\n  * Read the documentation at https:\/\/symfony.com\/doc<\/pre>\n<p>TEST IT VIA INTERNAL PHP SERVER:<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~\/Documents\/works\/symfony4$ cd symfony4test\/\r\nteddy@teddy:~\/Documents\/works\/symfony4\/symfony4test$ php -S 127.0.0.1:8000 -t public<\/pre>\n<p>OPEN VIA BROWSER: http:\/\/127.0.0.1:8000\/ &#8211;&gt; WORKS<\/p>\n<p>CREATE A VIRTUALHOST<br \/>\n1. CREATE A NEW conf FILE<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy:~\/Documents\/works\/symfony4$ sudo gedit \/etc\/apache2\/sites-available\/symfony4test.test.conf<\/pre>\n<p>HERE IS THE CONTENT:<\/p>\n<pre class=\"lang:default decode:true \">&lt;VirtualHost symfony4test.test:80&gt;\r\n    ServerName symfony4test.test\r\n    ServerAlias www.symfony4test.test\r\n    DocumentRoot \"\/home\/teddy\/Documents\/works\/symfony4\/symfony4test\/public\"\r\n    &lt;Directory \"\/home\/teddy\/Documents\/works\/symfony4\/symfony4test\/public\"&gt;\r\n        AllowOverride All\r\n        Allow from All\r\n        Require all granted\r\n    &lt;\/Directory&gt;\r\n    ErrorLog \/var\/log\/apache2\/symfony4test_error.log\r\n    CustomLog \/var\/log\/apache2\/symfony4test_access.log combined\r\n&lt;\/VirtualHost&gt;<\/pre>\n<p>2. Enable the site<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~\/Documents\/works\/symfony4$ sudo a2ensite symfony4test.test<\/pre>\n<p>3. Register the site in \/etc\/hosts<\/p>\n<pre class=\"lang:default decode:true\">teddy@teddy:~\/Documents\/works\/symfony4$ sudo gedit \/etc\/hosts<\/pre>\n<p>HERE IS THE CONTENT:<\/p>\n<pre class=\"lang:default decode:true \">...\r\n127.0.0.1   symfony4test.test\r\n...<\/pre>\n<p>RESTART THE APACHE2 SERVER: sudo systemctl restart apache2<br \/>\nTEST IT VIA BROWSER: http:\/\/symfony4test.test\/ &#8211;&gt; WORKS<\/p>\n<p>ROUTE AND CONTROLLER (ref: https:\/\/knpuniversity.com\/screencast\/symfony\/route-controller#play)<br \/>\nModify \/config\/routes.yaml FILE TO UNCOMMENT THE CONTENT AND CHANGE THE CONTROLLER AND THE ACTION:<\/p>\n<pre class=\"lang:default decode:true\">index:\r\n    path: \/\r\n    controller: App\\Controller\\ArticleController::homepage\r\n<\/pre>\n<p>CREATE THE CONTROLLER &#8216;ArticleController&#8217;\u00a0 FILE AND CLASS. ALSO CREATE &#8216;homepage&#8217; FUNCTION IN\u00a0\/src\/Controller\/ArticleController.php:<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace App\\Controller;\r\n\r\nuse Symfony\\Component\\HttpFoundation\\Response;\r\n\r\nclass ArticleController {\r\n    public function homepage(){\r\n        return new Response(\"Hello, This is my first page!\");\r\n    }\r\n}<\/pre>\n<p>REFRESH THE BROWSER: http:\/\/symfony4test.test\/<br \/>\nIT&#8217;D SHOW &#8216;Hello, This is my first page!&#8217; TEXT!<\/p>\n<p>USE ANNOTATION ROUTE (PREFERRED) INSTEAD OF MODIFYING \/config\/routes.yaml<br \/>\nUSE COMPOSER TO INSTALL IT &#8216;composer require annotations&#8217;<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~\/Documents\/works\/symfony4\/symfony4test$ composer require annotations\r\nUsing version ^5.1 for sensio\/framework-extra-bundle\r\n.\/composer.json has been updated\r\nLoading composer repositories with package information\r\nUpdating dependencies (including require-dev)\r\nPackage operations: 7 installs, 0 updates, 0 removals\r\n  - Installing doctrine\/lexer (v1.0.1): Loading from cache\r\n  - Installing doctrine\/inflector (v1.3.0): Loading from cache\r\n  - Installing doctrine\/collections (v1.5.0): Loading from cache\r\n  - Installing doctrine\/cache (v1.7.1): Loading from cache\r\n  - Installing doctrine\/annotations (v1.6.0): Loading from cache\r\n  - Installing doctrine\/common (v2.8.1): Loading from cache\r\n  - Installing sensio\/framework-extra-bundle (v5.1.4): Loading from cache\r\nWriting lock file\r\nGenerating autoload files\r\nSymfony operations: 2 recipes (1eee9f4a26353ed781b272153022870a)\r\n  - Configuring doctrine\/annotations (&gt;=1.0): From github.com\/symfony\/recipes:master\r\n  - Configuring sensio\/framework-extra-bundle (&gt;=4.0): From github.com\/symfony\/recipes:master\r\nExecuting script cache:clear [OK]\r\nExecuting script assets:install --symlink --relative public [OK]\r\n\r\nSome files may have been created or updated to configure your new packages.\r\nPlease review, edit and commit them: these files are yours.<\/pre>\n<p>BECAUSE WE DONT NEED routes.yaml, COMMENT THE CONTENT BACK (IN \/config\/routes.yaml):<\/p>\n<pre class=\"lang:default decode:true \">#index:\r\n#    path: \/\r\n#    controller: App\\Controller\\ArticleController::homepage<\/pre>\n<p>MODIFY AGAIN \/src\/Controller\/ArticleController.php AND ADD THE ANNOTATION<\/p>\n<pre class=\"lang:default decode:true \">...\r\nuse Sensio\\Bundle\\FrameworkExtraBundle\\Configuration\\Route;\r\nuse Symfony\\Component\\HttpFoundation\\Response;\r\n\r\nclass ArticleController {\r\n    \/**\r\n     * @Route(\"\/\")\r\n     *\/\r\n    public function homepage(){\r\n        return new Response(\"Hello, This is my first page with annotations!\");\r\n    }\r\n}<\/pre>\n<p>REFRESH THE BROWSER: http:\/\/symfony4test.test\/<br \/>\nIT&#8217;D SHOW &#8216;Hello, This is my first page\u00a0with annotations!&#8217; TEXT!<\/p>\n<p>FANCY WILDCARD ANNOTATION ROUTE<br \/>\nFOR EXAMPLE ADD THIS NEW ROUTE IN \/src\/Controller\/ArticleController.php:<\/p>\n<pre class=\"lang:default decode:true \">...\r\nclass ArticleController {\r\n    ...\r\n    \r\n    \/**\r\n     * @Route(\"\/news\/why-asteroids-taste-like-bacon\")\r\n     *\/\r\n    public function show()\r\n    {\r\n        return new Response('Future page to show one space article!');\r\n    }\r\n}<\/pre>\n<p>WE SHOULD OPEN IT ON BROWSER LIKE THIS: http:\/\/symfony4test.test\/news\/why-asteroids-taste-like-bacon<br \/>\nBUT I GOT &#8216;Not found&#8217; ERROR.<br \/>\nOK. I NEED TO RUN &#8216;composer require apache-pack&#8217;<\/p>\n<pre class=\"lang:default decode:true \">teddy@teddy:~\/Documents\/works\/symfony4\/symfony4test$ composer require symfony\/apache-pack\r\nUsing version ^1.0 for symfony\/apache-pack\r\n.\/composer.json has been updated\r\nLoading composer repositories with package information\r\nUpdating dependencies (including require-dev)\r\nPackage operations: 1 install, 0 updates, 0 removals\r\n  - Installing symfony\/apache-pack (v1.0.1): Downloading (100%)         \r\nWriting lock file\r\nGenerating autoload files\r\nSymfony operations: 1 recipe (8cd75672403cdc5a8053943729bbe843)\r\n  -  WARNING  symfony\/apache-pack (&gt;=1.0): From github.com\/symfony\/recipes-contrib:master\r\n    The recipe for this package comes from the \"contrib\" repository, which is open to community contributions.\r\n    Do you want to execute this recipe?\r\n    [y] Yes\r\n    [n] No\r\n    [a] Yes for all packages, only for the current installation session\r\n    [p] Yes permanently, never ask again for this project\r\n    (defaults to n): \r\nExecuting script cache:clear [OK]\r\nExecuting script assets:install --symlink --relative public [OK]<\/pre>\n<p>REFRESH BROWSER! &#8211;&gt; STILL NOT WORKING<br \/>\nI NEED TO CREATE .htaccess FILE in \/public\/ DIR (ref: https:\/\/knpuniversity.com\/screencast\/symfony\/route-controller#play<br \/>\nAND THE .htaccess CONTENT GOT FROM https:\/\/raw.githubusercontent.com\/symfony\/recipes-contrib\/master\/symfony\/apache-pack\/1.0\/public\/.htaccess):<br \/>\n\/public\/.htaccess<\/p>\n<pre class=\"lang:default decode:true \"># Use the front controller as index file. It serves as a fallback solution when\r\n# every other rewrite\/redirect fails (e.g. in an aliased environment without\r\n# mod_rewrite). Additionally, this reduces the matching process for the\r\n# start page (path \"\/\") because otherwise Apache will apply the rewriting rules\r\n# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).\r\nDirectoryIndex index.php\r\n\r\n# By default, Apache does not evaluate symbolic links if you did not enable this\r\n# feature in your server configuration. Uncomment the following line if you\r\n# install assets as symlinks or if you experience problems related to symlinks\r\n# when compiling LESS\/Sass\/CoffeScript assets.\r\n# Options FollowSymlinks\r\n\r\n# Disabling MultiViews prevents unwanted negotiation, e.g. \"\/index\" should not resolve\r\n# to the front controller \"\/index.php\" but be rewritten to \"\/index.php\/index\".\r\n&lt;IfModule mod_negotiation.c&gt;\r\n    Options -MultiViews\r\n&lt;\/IfModule&gt;\r\n\r\n&lt;IfModule mod_rewrite.c&gt;\r\n    RewriteEngine On\r\n\r\n    # Determine the RewriteBase automatically and set it as environment variable.\r\n    # If you are using Apache aliases to do mass virtual hosting or installed the\r\n    # project in a subdirectory, the base path will be prepended to allow proper\r\n    # resolution of the index.php file and to redirect to the correct URI. It will\r\n    # work in environments without path prefix as well, providing a safe, one-size\r\n    # fits all solution. But as you do not need it in this case, you can comment\r\n    # the following 2 lines to eliminate the overhead.\r\n    RewriteCond %{REQUEST_URI}::$1 ^(\/.+)\/(.*)::\\2$\r\n    RewriteRule ^(.*) - [E=BASE:%1]\r\n\r\n    # Sets the HTTP_AUTHORIZATION header removed by Apache\r\n    RewriteCond %{HTTP:Authorization} .\r\n    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]\r\n\r\n    # Redirect to URI without front controller to prevent duplicate content\r\n    # (with and without `\/index.php`). Only do this redirect on the initial\r\n    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an\r\n    # endless redirect loop (request -&gt; rewrite to front controller -&gt;\r\n    # redirect -&gt; request -&gt; ...).\r\n    # So in case you get a \"too many redirects\" error or you always get redirected\r\n    # to the start page because your Apache does not expose the REDIRECT_STATUS\r\n    # environment variable, you have 2 choices:\r\n    # - disable this feature by commenting the following 2 lines or\r\n    # - use Apache &gt;= 2.3.9 and replace all L flags by END flags and remove the\r\n    #   following RewriteCond (best solution)\r\n    RewriteCond %{ENV:REDIRECT_STATUS} ^$\r\n    RewriteRule ^index\\.php(?:\/(.*)|$) %{ENV:BASE}\/$1 [R=301,L]\r\n\r\n    # If the requested filename exists, simply serve it.\r\n    # We only want to let Apache serve files and not directories.\r\n    RewriteCond %{REQUEST_FILENAME} -f\r\n    RewriteRule ^ - [L]\r\n\r\n    # Rewrite all other queries to the front controller.\r\n    RewriteRule ^ %{ENV:BASE}\/index.php [L]\r\n&lt;\/IfModule&gt;\r\n\r\n&lt;IfModule !mod_rewrite.c&gt;\r\n    &lt;IfModule mod_alias.c&gt;\r\n        # When mod_rewrite is not available, we instruct a temporary redirect of\r\n        # the start page to the front controller explicitly so that the website\r\n        # and the generated links can still be used.\r\n        RedirectMatch 307 ^\/$ \/index.php\/\r\n        # RedirectTemp cannot be used instead\r\n    &lt;\/IfModule&gt;\r\n&lt;\/IfModule&gt;<\/pre>\n<p>REFRESH BROWSER: http:\/\/symfony4test.test\/news\/why-asteroids-taste-like-bacon &#8211;&gt; WORKS<br \/>\nIT&#8217;D PRINT &#8216;Future page to show one space article!&#8217;<\/p>\n<p>USE ANNOTATION WITH PARAMETER<br \/>\nMODIFY \/src\/Controller\/ArticleController.php:<\/p>\n<pre class=\"lang:default decode:true \">...\r\nclass ArticleController {\r\n    ...\r\n    \r\n    \/**\r\n     * @Route(\"\/pages\/{slug}\")\r\n     *\/\r\n    public function page($slug)\r\n    {\r\n        return new Response(sprintf('Here is the page \"%s\" content!', $slug));\r\n    }\r\n}<\/pre>\n<p>REFRESH BROWSER: http:\/\/symfony4test.test\/pages\/test-page &#8211;&gt; WORKS<br \/>\nIT&#8217;D PRINT &#8216;Here is the page &#8220;test-page&#8221; content!&#8217;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>From this: CREATE SYMFONY 4 PROJECT (ref: https:\/\/symfony.com\/doc\/current\/setup.html &#8211;&gt; DONT USE THE INSTRUCTION &#8216;symfony\/website-skeleton&#8217;. USE &#8216;symfony\/skeleton&#8217; FROM https:\/\/knpuniversity.com\/screencast\/symfony\/setup#play INSTEAD) 1. CREATE A NEW DIR &#8216;symfony4&#8217; IN Documents\/works\/ DIR 2. GO INTO IT AND USE &#8216;composer&#8217; TO CREATE THE PROJECT SKELETON: teddy@teddy:~\/Documents\/works\/symfony4$ composer create-project symfony\/skeleton symfony4test Installing symfony\/skeleton (v4.0.5) &#8211; Installing symfony\/skeleton (v4.0.5): Downloading (100%) Created &hellip; <a href=\"https:\/\/myprojects.advchaweb.com\/index.php\/2018\/01\/31\/symfony-4-tutorial\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Symfony 4 Tutorial&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,33,79],"tags":[],"class_list":["post-2695","post","type-post","status-publish","format-standard","hentry","category-php-2","category-symfony","category-symfony-4"],"_links":{"self":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/2695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/comments?post=2695"}],"version-history":[{"count":8,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/2695\/revisions"}],"predecessor-version":[{"id":2704,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/posts\/2695\/revisions\/2704"}],"wp:attachment":[{"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/media?parent=2695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/categories?post=2695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myprojects.advchaweb.com\/index.php\/wp-json\/wp\/v2\/tags?post=2695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}