Symfony 5: The Fast Track Fabien Potencier https://fabien.potencier.org/ @fabpot @fabpot Symfony 5: The Fast Track ISBN-13: 978-2-918390-37-4 v1.0.6 — Generated on February 10, 2020 Symfony SAS 92-98, boulevard Victor Hugo 92 110 Clichy France This work is licensed under the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0)” license (https://creativecommons.org/licenses/by-nc-sa/4.0/). Below is a human-readable summary of (and not a substitute for) the license (https://creativecommons.org/ licenses/by-nc-sa/4.0/legalcode). You are free to Share— copy and redistribute the material in any medium or format Adapt— remix, transform, and build upon the material • Attribution:Youmustgiveappropriatecredit,providealinktothelicense,andindicateifchangeswere made.Youmaydosoinanyreasonablemanner,butnotinanywaythatsuggeststhelicensorendorses you or your use. • Non Commercial: You may not use the material for commercial purposes. • ShareAlike:Ifyouremix,transform,orbuilduponthematerial,youmustdistributeyourcontributions under the same license as the original. Theinformationinthisbookisdistributedonan“asis”basis,withoutwarranty.Althougheveryprecaution has been taken in the preparation of this work, neither the author(s) nor Symfony shall have any liability to anypersonorentitywithrespecttoanylossordamagecausedorallegedtobecauseddirectlyorindirectlyby the information contained in this work. If you find typos or errors, feel free to report them at [email protected]. This book is continuously updated based on user feedback. Contents at a Glance Step 0: What is it about?..................................................................................25 Step 1: Checking your Work Environment........................................................29 Step 2: Introducing the Project.........................................................................33 Step 3: Going from Zero to Production.............................................................39 Step 4: Adopting a Methodology ......................................................................47 Step 5: Troubleshooting Problems....................................................................49 Step 6: Creating a Controller...........................................................................57 Step 7: Setting up a Database...........................................................................65 Step 8: Describing the Data Structure...............................................................71 Step 9: Setting up an Admin Backend ...............................................................85 Step 10: Building the User Interface .................................................................93 Step 11: Branching the Code..........................................................................107 Step 12: Listening to Events...........................................................................115 Step 13: Managing the Lifecycle of Doctrine Objects.......................................121 Step 14: Accepting Feedback with Forms........................................................131 Step 15: Securing the Admin Backend.............................................................147 Step 16: Preventing Spam with an API ...........................................................155 Step 17: Testing............................................................................................163 Step 18: Going Async....................................................................................179 Step 19: Making Decisions with a Workflow...................................................197 Step 20: Emailing Admins .............................................................................203 Step 21: Caching for Performance..................................................................217 Step 22: Styling the User Interface with Webpack............................................233 Step 23: Resizing Images...............................................................................239 Step 24: Running Crons.................................................................................245 Step 25: Notifying by all Means.....................................................................251 Step 26: Exposing an API with API Platform..................................................267 v Step 27: Building an SPA...............................................................................277 Step 28: Localizing an Application.................................................................295 Step 29: Managing Performance....................................................................309 Step 30: Discovering Symfony Internals..........................................................319 Step 31: What’s Next?...................................................................................325 vi Table of Contents Step 0: What is it about?..................................................................................25 Step 1: Checking your Work Environment........................................................29 1.1: A Computer.........................................................................................29 1.2: Opinionated Choices.............................................................................30 1.3: IDE.....................................................................................................30 1.4: Terminal..............................................................................................30 1.5: Git ......................................................................................................30 1.6: PHP ....................................................................................................31 1.7: Composer ............................................................................................31 1.8: Docker and Docker Compose................................................................31 1.9: Symfony CLI........................................................................................32 Step 2: Introducing the Project.........................................................................33 2.1: Revealing the Project ............................................................................33 2.2: Learning is Doing.................................................................................34 2.3: Looking at the Final Infrastructure Diagram..........................................34 2.4: Getting the Project Source Code.............................................................36 2.5: Navigating the Source Code ..................................................................37 Step 3: Going from Zero to Production.............................................................39 3.1: Initializing the Project...........................................................................40 3.2: Creating some Public Resources.............................................................41 3.3: Launching a Local Web Server..............................................................42 3.4: Adding a favicon...................................................................................43 3.5: Preparing for Production.......................................................................43 3.6: Going to Production .............................................................................44 ix Step 4: Adopting a Methodology ......................................................................47 4.1: Implementing a Git Strategy..................................................................47 4.2: Deploying to Production Continuously...................................................48 Step 5: Troubleshooting Problems....................................................................49 5.1: Installing more Dependencies................................................................49 5.2: Understanding Symfony Environments...................................................50 5.3: Managing Environment Configurations..................................................51 5.4: Logging all the Things...........................................................................51 5.5: Discovering the Symfony Debugging Tools.............................................52 5.6: Configuring your IDE...........................................................................55 5.7: Debugging Production ..........................................................................55 Step 6: Creating a Controller...........................................................................57 6.1: Being Lazy with the Maker Bundle ........................................................57 6.2: Choosing a Configuration Format..........................................................58 6.3: Generating a Controller........................................................................59 6.4: Adding an Easter Egg............................................................................61 Step 7: Setting up a Database...........................................................................65 7.1: Adding PostgreSQL to Docker Compose ................................................65 7.2: Starting Docker Compose......................................................................66 7.3: Accessing the Local Database................................................................67 7.4: Adding PostgreSQL to SymfonyCloud....................................................67 7.5: Accessing the SymfonyCloud Database ..................................................69 7.6: Exposing Environment Variables...........................................................70 Step 8: Describing the Data Structure...............................................................71 8.1: Configuring Doctrine ORM...................................................................71 8.2: Understanding Symfony Environment Variable Conventions...................72 8.3: Changing the Default DATABASE_URL Value in .env ...........................73 8.4: Creating Entity Classes.........................................................................73 8.5: Linking Entities....................................................................................77 8.6: Adding more Properties.........................................................................81 8.7: Migrating the Database ........................................................................81 x