Skip to content

Instantly share code, notes, and snippets.

@MarcinGladkowski
Last active December 16, 2021 11:16
Show Gist options
  • Save MarcinGladkowski/caf51b67c5d1c49e2703dfc2b9a3ec31 to your computer and use it in GitHub Desktop.
Save MarcinGladkowski/caf51b67c5d1c49e2703dfc2b9a3ec31 to your computer and use it in GitHub Desktop.
Symfony 5 cheatsheet

Commands

Services/Autowiring

  • List services which you can use with type hinting to directly fetch them
php bin/console debug:autowiring

Example output

...
Symfony\Component\String\Slugger\SluggerInterface (slugger)
 
Covers most simple to advanced caching needs.
Symfony\Contracts\Cache\CacheInterface (cache.app)
 
Allows invalidating cached items using tags.
Symfony\Contracts\Cache\TagAwareCacheInterface (cache.app.taggable)
...

(cache.app.taggable) is an id of service

Autowiring means you can use TagAwareCacheInterface or CacheInterface to inject for example in your controller

public function index(TagAwareCacheInterface $service) 
{
 // ...
} 

  • List Twig service functions In this case twig
php bin/console debug:twig
  • Messenger

List all classes connected with command bus php bin/console debug:messenger


  • Dump selected configuration
    Shows examples of configuration. The real configuration is available by debug:config.
php bin/console config:dump KnpMarkdownBundle
  • Show direct/real configuration
    With cache key for specified part of config. You can use any which exist.
php bin/console debug:config FrameworkBundle cache

  • List all available services There are a full list of services, but not each can be autowiring by type hinting
php bin/console debug:container
  • Parameters

In config:

parameters:
    cache_adapter: cache.adaper.filesystem

Usage in another yaml file using % sign.

framework:
    cache:
        app: '%cache_adapter%'
  • List all parameters in app:
php bin/console debug:container --parameters

Environments

  • Don't commit sensitive data in .env. Just create env.local for development on local environment. Every value puts in this file overrides .env. .local files are ignore by GIT.

  • Display envs

php bin/console debug:container --env-vars

Tests

  • The environments variables which should use with your tests should be stored in for e.g file .env.test
  • To run Symfony environments with 100% sure with testing envs just run $ APP_ENV=test php bin/phpunit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment