├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | This work is licensed under a Creative Commons Attribution 4.0 International License: 2 | http://creativecommons.org/licenses/by/4.0/ 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Symfony Cheat Sheet 2 | 3 | Useful practical commands, code and things you will frequently need for working with Symfony framework. 4 | 5 | ## Table of contents 6 | 7 | * [Symfony Installer](#symfony-installer) 8 | * [Composer](#composer) 9 | * [Console](#console) 10 | * [General](#general) 11 | * [Cache](#cache) 12 | * [Bundle](#bundle) 13 | * [Routing](#routing) 14 | * [Doctrine](#doctrine) 15 | * [Twig](#twig) 16 | * [Controller](#controller) 17 | 18 | ## Symfony Installer 19 | 20 | ```bash 21 | # Install Symfony Installer 22 | curl -LsS http://symfony.com/installer > symfony.phar 23 | sudo mv symfony.phar /usr/local/bin/symfony 24 | chmod a+x /usr/local/bin/symfony 25 | ``` 26 | 27 | ```bash 28 | # Create a new project 29 | symfony new my_project 30 | ``` 31 | 32 | ```bash 33 | # Update the Symfony installer 34 | symfony self-update 35 | ``` 36 | 37 | ```bash 38 | # List help 39 | symfony about 40 | ``` 41 | 42 | ## Composer 43 | 44 | ```bash 45 | # Create new project with Composer 46 | composer create-project symfony/framework-standard-edition ~2.6 47 | ``` 48 | 49 | ```bash 50 | # Add package to project 51 | composer require vendor/library 52 | ``` 53 | 54 | ```bash 55 | # Update all dependencies of current project 56 | composer update 57 | ``` 58 | 59 | ```bash 60 | # Install dependencies for current project with versions defined in composer.lock 61 | composer install 62 | ``` 63 | 64 | ## Console 65 | 66 | ### General 67 | 68 | ```bash 69 | # List available commands and show the Symfony version 70 | php app/console 71 | ``` 72 | 73 | ```bash 74 | # Display help for given command 75 | php app/console help [command] 76 | ``` 77 | 78 | ```bash 79 | # Display all configured public services 80 | php app/console container:debug [--show-private] [service_name] 81 | ``` 82 | 83 | ```bash 84 | # Dump all assets to the filesystem 85 | php app/console assetic:dump [--watch] [--force] [--period=...] [write_to] 86 | ``` 87 | 88 | ```bash 89 | # Dump the default configuration for an extension/bundle 90 | php app/console config:dump-reference 91 | ``` 92 | 93 | ```bash 94 | # Send messages from spool 95 | php app/console swiftmailer:spool:send [--message-limit=...] [--time-limit=...] [--recover-timeout=...] 96 | ``` 97 | 98 | ```bash 99 | # Extract translation strings from templates of a given bundle. 100 | # It can display them or merge the new ones into the translation files 101 | php app/console translation:update [--prefix=...] [--update-format=...] [--dump-messages] [--force] 102 | ``` 103 | 104 | ```bash 105 | # Lint a template and outputs to stdout the first encountered syntax error 106 | php app/console twig:lint ` 107 | ``` 108 | 109 | ### Cache 110 | 111 | ```bash 112 | # Clear the cached information 113 | php app/console cache:clear [--no-warmup] [--no-optional-warmers] 114 | ``` 115 | 116 | ```bash 117 | # Warm up an empty cache 118 | php app/console cache:warmup [--no-optional-warmers]` 119 | ``` 120 | 121 | ### Bundle 122 | 123 | ```bash 124 | # Install bundles web assets under a public web directory 125 | php app/console assets:install [--symlink] [--relative] 126 | ``` 127 | 128 | ```bash 129 | # Generate a bundle 130 | php app/console generate:bundle [--namespace=...] [--dir=...] [--bundle-name=...] [--format=...] [--structure] 131 | ``` 132 | 133 | ### Routing 134 | 135 | ```bash 136 | # Display current routes for application 137 | php app/console router:debug [route_name] 138 | ``` 139 | 140 | ```bash 141 | # Dump all routes as Apache rewrite rules 142 | php app/console router:dump-apache [--base-uri=...] [script_name] 143 | ``` 144 | 145 | ```bash 146 | # Debug routes by simulating a path info match 147 | php app/console router:match 148 | ``` 149 | 150 | ```bash 151 | # Display current exposed routes for an application 152 | php app/console fos:js-routing:debug 153 | ``` 154 | 155 | ### Doctrine 156 | 157 | ```bash 158 | # Clear all metadata cache for an entity manager 159 | php app/console doctrine:cache:clear-metadata [--em=...] [--flush] 160 | ``` 161 | 162 | ```bash 163 | # Clear all query cache for an entity manager 164 | php app/console doctrine:cache:clear-query [--em=...] [--flush] 165 | ``` 166 | 167 | ```bash 168 | # Clear all result cache for an entity manager 169 | php app/console doctrine:cache:clear-result [--em=...] [--flush] 170 | ``` 171 | 172 | ```bash 173 | # Create the configured databases 174 | php app/console doctrine:database:create [--connection=...] 175 | ``` 176 | 177 | ```bash 178 | # Drop the configured databases 179 | php app/console doctrine:database:drop [--connection=...] [--force] 180 | ``` 181 | 182 | ```bash 183 | # Convert mapping information between supported formats 184 | php app/console doctrine:mapping:convert [--filter=...] [--force] [--from-database] [--extend=...] [--num-spaces=...] [--namespace=...] [--em=...] 185 | ``` 186 | 187 | ```bash 188 | # Import mapping information from an existing database 189 | php app/console doctrine:mapping:import [--em=...] [--filter=...] [--force] 190 | ``` 191 | 192 | ```bash 193 | # Show basic information about all mapped entities 194 | php app/console doctrine:mapping:info [--em=...] 195 | ``` 196 | 197 | ```bash 198 | # Generate a new Doctrine entity inside a bundle 199 | php app/console doctrine:generate:entity [--entity=...] [--fields=...] [--format=...] [--with-repository] 200 | ``` 201 | 202 | ```bash 203 | # Generate entity classes and method stubs from your mapping information 204 | php app/console doctrine:generate:entities [--path=...] [--no-backup] 205 | ``` 206 | 207 | ```bash 208 | # Generate a form class based on a Doctrine entity 209 | php app/console doctrine:generate:form 210 | ``` 211 | 212 | ```bash 213 | # Generate a CRUD based on a Doctrine entity 214 | php app/console doctrine:generate:crud [--entity=...] [--route-prefix=...] [--with-write] [--format=...] 215 | ``` 216 | 217 | ```bash 218 | # Execute arbitrary DQL directly from the command line 219 | php app/console doctrine:query:dql [--hydrate=...] [--first-result=...] [--max-result=...] [--depth=...] [--em=...] 220 | ``` 221 | 222 | ```bash 223 | # Execute arbitrary SQL directly from the command line 224 | php app/console doctrine:query:sql [--depth=...] [--connection=...] 225 | ``` 226 | 227 | ```bash 228 | # Executes (or dumps) the SQL needed to generate the database schema 229 | php app/console doctrine:schema:create [--dump-sql] [--em=...] 230 | ``` 231 | 232 | ```bash 233 | # Execute (or dump) the SQL needed to drop the current database schema 234 | php app/console doctrine:schema:drop [--dump-sql] [--force] [--full-database] [--em=...] 235 | ``` 236 | 237 | ```bash 238 | # Executes (or dumps) the SQL needed to update the database schema to match the current mapping metadata 239 | php app/console doctrine:schema:update [--complete] [--dump-sql] [--force] [--em=...] 240 | ``` 241 | 242 | ```bash 243 | # Validate the Doctrine mapping files 244 | php app/console doctrine:schema:validate [--em=...] 245 | ``` 246 | 247 | ```bash 248 | # Ensure that Doctrine is properly configured for a production environment 249 | php app/console doctrine:ensure-production-settings [--complete] [--em=...] 250 | ``` 251 | 252 | ```bash 253 | # Load data fixtures to your database 254 | php app/console doctrine:fixtures:load [--fixtures=...] [--append] [--em=...] [--purge-with-truncate] 255 | ``` 256 | 257 | ## Twig 258 | 259 | ```twig 260 | {# Echo variable with html escaping #} 261 | {{ content }} 262 | ``` 263 | 264 | ```twig 265 | {# Echo raw variable and disable html escaping #} 266 | {{ content|raw }} 267 | ``` 268 | 269 | ```twig 270 | {# Extend template #} 271 | {% extends '::base.html.twig' %} 272 | ``` 273 | 274 | ## Controller 275 | 276 | ```php 277 | use Symfony\Component\HttpFoundation\Response; 278 | 279 | public function helloAction() 280 | { 281 | return new Response('Hello world!'); 282 | } 283 | ``` --------------------------------------------------------------------------------