ENGLISH

Symfony 6 - The Fast Track (for Symfony 6.2)

Book information

Publisher
Leanpub
Year
2022
ISBN
9782918390374
Language
english
Format
PDF
Filesize
17 MB (17396954 bytes)
Edition
6.2
Pages
331\348
Time added
2022-12-22 10:35:08

Description

This book - written by Symfony's creator - lays out a pragmatic approach to developing web applications with Symfony: from scratch to production. Whether you are discovering Symfony for the first time or refreshing your knowledge, this practical guide provides the definitive introduction to modern Symfony applications. About the Book Is it possible to quickly build a modern web application and love the process? With Symfony, you can. This book - written by Symfony's creator - lays out a pragmatic approach to developing web applications with Symfony: from scratch to production. Whether you are discovering Symfony for the first time or refreshing your knowledge, this practical guide provides the definitive introduction to modern Symfony applications. Learn how to combine Symfony with Docker, APIs, notifications, queues & asynchronous tasks, Webpack, Single-Page Applications, and other powerful tools. By the end of this book, you'll have the confidence, understanding and a clear path to dive in and start building your next application with Symfony. Step 0: What is it about?..................................................................................21 Step 1: Checking your Work Environment........................................................25 1.1: A Computer .........................................................................................25 1.2: Opinionated Choices.............................................................................26 1.3: IDE .....................................................................................................26 1.4: Terminal..............................................................................................26 1.5: Git ......................................................................................................27 1.6: PHP ....................................................................................................27 1.7: Composer ............................................................................................27 1.8: NodeJS ................................................................................................28 1.9: Docker and Docker Compose ................................................................28 1.10: Symfony CLI......................................................................................28 Step 2: Introducing the Project.........................................................................31 2.1: Revealing the Project ............................................................................31 2.2: Learning is Doing .................................................................................32 2.3: Looking at the Final Infrastructure Diagram ..........................................32 2.4: Getting the Project Source Code.............................................................34 2.5: Navigating the Source Code ..................................................................35 Step 3: Going from Zero to Production.............................................................37 3.1: Initializing the Project...........................................................................38 3.2: Creating some Public Resources.............................................................40 3.3: Launching a Local Web Server..............................................................40 3.4: Preparing for Production.......................................................................41 3.5: Going to Production .............................................................................42 Step 4: Adopting a Methodology ......................................................................45 4.1: Implementing a Git Strategy..................................................................45 4.2: Deploying to Production Continuously...................................................46 Step 5: Troubleshooting Problems....................................................................47 5.1: Discovering the Symfony Debugging Tools.............................................47 5.2: Understanding Symfony Environments...................................................49 5.3: Managing Environment Configurations..................................................50 5.4: Configuring your IDE...........................................................................51 5.5: Debugging Production ..........................................................................51 Step 6: Creating a Controller...........................................................................53 6.1: Being Lazy with the Maker Bundle ........................................................53 6.2: Choosing a Configuration Format..........................................................54 6.3: Generating a Controller........................................................................54 6.4: Adding an Easter Egg............................................................................57 6.5: Debugging Variables.............................................................................58 Step 7: Setting up a Database...........................................................................61 7.1: Adding PostgreSQL to Docker Compose ................................................61 7.2: Starting Docker Compose......................................................................62 7.3: Accessing the Local Database ................................................................63 7.4: Dumping and Restoring Database Data .................................................64 7.5: Adding PostgreSQL to Platform.sh ........................................................64 7.6: Accessing the Platform.sh Database .......................................................65 7.7: Exposing Environment Variables...........................................................66 7.8: Describing your Infrastructure ..............................................................66 Step 8: Describing the Data Structure...............................................................69 8.1: Configuring Doctrine ORM...................................................................69 8.2: Understanding Symfony Environment Variable Conventions...................70 8.3: Changing the Default DATABASE_URL Value in .env ...........................70 8.4: Creating Entity Classes.........................................................................71 8.5: Linking Entities....................................................................................74 8.6: Adding more Properties.........................................................................78 8.7: Migrating the Database ........................................................................79 8.8: Updating the Local Database ................................................................80 8.9: Updating the Production Database ........................................................80 Step 9: Setting up an Admin Backend ...............................................................83 9.1: Installing more Dependencies ................................................................83 9.2: Configuring EasyAdmin........................................................................84 9.3: Customizing EasyAdmin .......................................................................90 Step 10: Building the User Interface .................................................................95 10.1: Using Twig for the Templates..............................................................95 10.2: Using Twig in a Controller..................................................................97 10.3: Creating the Page for a Conference ......................................................98 10.4: Linking Pages Together..................................................................... 100 10.5: Paginating the Comments.................................................................. 101 10.6: Refactoring the Controller................................................................. 105 Step 11: Branching the Code.......................................................................... 107 11.1: Adopting a Git Workflow.................................................................. 107 11.2: Creating Branches ............................................................................ 108 11.3: Storing Sessions in the Database ........................................................ 108 11.4: Deploying a Branch .......................................................................... 110 11.5: Debugging Production Deployments before Deploying ........................ 112 11.6: Testing Production Deployments before Deploying.............................. 112 11.7: Merging to Production ...................................................................... 113 11.8: Cleaning up...................................................................................... 113 Step 12: Listening to Events........................................................................... 115 12.1: Adding a Website Header.................................................................. 115 12.2: Discovering Symfony Events.............................................................. 117 12.3: Implementing a Subscriber ................................................................ 117 12.4: Sorting Conferences by Year and City ................................................ 119 Step 13: Managing the Lifecycle of Doctrine Objects ....................................... 121 13.1: Defining Lifecycle Callbacks.............................................................. 121 13.2: Adding Slugs to Conferences.............................................................. 122 13.3: Generating Slugs .............................................................................. 124 13.4: Defining a Complex Lifecycle Callback .............................................. 125 13.5: Configuring a Service in the Container............................................... 126 13.6: Using Slugs in the Application ........................................................... 127 Step 14: Accepting Feedback with Forms ........................................................ 131 14.1: Generating a Form Type ................................................................... 131 14.2: Displaying a Form............................................................................ 132 14.3: Customizing a Form Type ................................................................. 134 14.4: Validating Models............................................................................. 136 14.5: Handling a Form .............................................................................. 138 14.6: Uploading Files ................................................................................ 139 14.7: Debugging Forms ............................................................................. 141 14.8: Displaying Uploaded Photos in the Admin Backend ............................ 143 14.9: Excluding Uploaded Photos from Git ................................................. 144 14.10: Storing Uploaded Files on Production Servers ................................... 144 Step 15: Securing the Admin Backend............................................................. 147 15.1: Defining a User Entity ...................................................................... 147 15.2: Generating a Password for the Admin User ........................................ 149 15.3: Creating an Admin ........................................................................... 150 15.4: Configuring the Security Authentication............................................. 150 15.5: Adding Authorization Access Control Rules........................................ 152 15.6: Authenticating via the Login Form..................................................... 152 Step 16: Preventing Spam with an API ........................................................... 155 16.1: Signing up on Akismet....................................................................... 155 16.2: Depending on Symfony HTTPClient Component ................................ 155 16.3: Designing a Spam Checker Class ....................................................... 156 16.4: Using Environment Variables............................................................ 157 16.5: Storing Secrets.................................................................................. 158 16.6: Checking Comments for Spam........................................................... 159 16.7: Managing Secrets in Production......................................................... 160 Step 17: Testing ............................................................................................ 163 17.1: Writing Unit Tests............................................................................ 163 17.2: Writing Functional Tests for Controllers ............................................ 165 17.3: Configuring the Test Environment ..................................................... 167 17.4: Working with a Test Database .......................................................... 167 17.5: Defining Fixtures.............................................................................. 168 17.6: Loading Fixtures .............................................................................. 170 17.7: Crawling a Website in Functional Tests.............................................. 171 17.8: Submitting a Form in a Functional Test.............................................. 172 17.9: Reloading the Fixtures ...................................................................... 174 17.10: Automating your Workflow with a Makefile..................................... 174 17.11: Resetting the Database after each Test ............................................. 175 17.12: Using a real Browser for Functional Tests......................................... 177 17.13: Choosing the Right Test Type .......................................................... 178 17.14: Running Black Box Functional Tests with Blackfire........................... 178 Step 18: Going Async .................................................................................... 181 18.1: Flagging Comments .......................................................................... 181 18.2: Understanding Messenger.................................................................. 184 18.3: Coding a Message Handler................................................................ 185 18.4: Going Async for Real ........................................................................ 188 18.5: Consuming Messages ........................................................................ 189 18.6: Running Workers in the Background.................................................. 190 18.7: Retrying Failed Messages .................................................................. 191 18.8: Running Workers on Platform.sh....................................................... 192 Step 19: Making Decisions with a Workflow................................................... 193 19.1: Describing Workflows....................................................................... 194 19.2: Using a Workflow............................................................................. 195 19.3: Finding Services from the Dependency Injection Container.................. 197 Step 20: Emailing Admins ............................................................................. 199 20.1: Setting an Email for the Admin.......................................................... 199 20.2: Sending a Notification Email ............................................................. 200 20.3: Extending the Notification Email Template ........................................ 201 20.4: Generating Absolute URLs in a Symfony Command ........................... 202 20.5: Wiring a Route to a Controller .......................................................... 203 20.6: Using a Mail Catcher........................................................................ 205 20.7: Accessing the Webmail...................................................................... 205 20.8: Managing Long-Running Scripts........................................................ 207 20.9: Sending Emails Asynchronously......................................................... 207 20.10: Testing Emails................................................................................ 208 20.11: Sending Emails on Platform.sh ........................................................ 209 Step 21: Caching for Performance.................................................................. 211 21.1: Adding HTTP Cache Headers............................................................ 211 21.2: Activating the Symfony HTTP Cache Kernel ...................................... 212 21.3: Avoiding SQL Requests with ESI ....................................................... 213 21.4: Purging the HTTP Cache for Testing.................................................. 218 21.5: Grouping similar Routes with a Prefix ............................................... 219 21.6: Caching CPU/Memory Intensive Operations ...................................... 220 21.7: Profiling and Comparing Performance ............................................... 222 21.8: Configuring a Reverse Proxy Cache on Production.............................. 222 21.9: Enabling ESI Support on Varnish....................................................... 223 21.10: Purging the Varnish Cache .............................................................. 224 Step 22: Styling the User Interface with Webpack............................................ 227 22.1: Using Sass........................................................................................ 228 22.2: Leveraging Bootstrap ........................................................................ 229 22.3: Styling the HTML............................................................................. 230 22.4: Building Assets ................................................................................. 230 Step 23: Resizing Images ............................................................................... 233 23.1: Optimizing Images with Imagine ....................................................... 234 23.2: Adding a new Step in the Workflow ................................................... 235 23.3: Storing Uploaded Data in Production................................................. 237 Step 24: Running Crons................................................................................. 239 24.1: Cleaning up Comments..................................................................... 239 24.2: Using Class Constants, Container Parameters, and Environment Variables.................................................................................................. 241 24.3: Creating a CLI Command ................................................................. 241 24.4: Setting up a Cron on Platform.sh ....................................................... 243 Step 25: Notifying by all Means ..................................................................... 245 25.1: Sending Web Application Notifications in the Browser ........................ 245 25.2: Notifying Admins by Email ............................................................... 249 25.3: Chatting with Admins....................................................................... 252 25.4: Going Asynchronous across the Board ............................................... 258 25.5: Notifying Users by Email .................................................................. 259 Step 26: Exposing an API with API Platform .................................................. 261 26.1: Installing API Platform ..................................................................... 261 26.2: Exposing an API for Conferences....................................................... 262 26.3: Exposing an API for Comments......................................................... 265 26.4: Restricting Comments exposed by the API.......................................... 267 26.5: Configuring CORS............................................................................ 268 Step 27: Building an SPA............................................................................... 269 27.1: Creating the Application ................................................................... 269 27.2: Creating the SPA Main Template....................................................... 271 27.3: Running an SPA in the Browser......................................................... 272 27.4: Adding a Router to handle States ....................................................... 273 27.5: Styling the SPA................................................................................. 275 27.6: Fetching Data from the API............................................................... 277 27.7: Deploying the SPA in Production ....................................................... 283 27.8: Configuring CORS for the SPA.......................................................... 284 27.9: Using Cordova to build a Smartphone Application .............................. 285 Step 28: Localizing an Application ................................................................. 287 28.1: Internationalizing URLs.................................................................... 287 28.2: Adding a Locale Switcher.................................................................. 290 28.3: Translating the Interface ................................................................... 292 28.4: Providing Translations...................................................................... 295 28.5: Translating Forms ............................................................................ 296 28.6: Localizing Dates............................................................................... 297 28.7: Translating Plurals........................................................................... 297 28.8: Updating Functional Tests................................................................. 299 Step 29: Managing Performance .................................................................... 301 29.1: Introducing Blackfire ........................................................................ 302 29.2: Setting Up the Blackfire Agent on Docker........................................... 303 29.3: Fixing a non-working Blackfire Installation........................................ 304 29.4: Configuring Blackfire in Production................................................... 304 29.5: Configuring Varnish for Blackfire ...................................................... 305 29.6: Profiling Web Pages.......................................................................... 306 29.7: Profiling API Resources..................................................................... 307 29.8: Comparing Performance ................................................................... 307 29.9: Writing Black Box Functional Tests ................................................... 307 29.10: Automating Performance Checks..................................................... 309 Step 30: Discovering Symfony Internals.......................................................... 311 30.1: Understanding Symfony Internals with Blackfire................................. 311 30.2: Using the Blackfire Debug Addon ...................................................... 315 30.3: Using a Step Debugger with Xdebug................................................... 316 Step 31: Using Redis to Store Sessions ............................................................ 321 Step 32: Using RabbitMQ as a Message Broker............................................... 325 32.1: Switching from PostgreSQL to RabbitMQ .......................................... 325 32.2: Adding RabbitMQ to the Docker Stack .............................................. 326 32.3: Restarting Docker Services................................................................ 326 32.4: Exploring the RabbitMQ Web Management Interface ......................... 327 32.5: Deploying RabbitMQ........................................................................ 328 Step 33: What’s Next?................................................................................... 331

Similar books