Injecting the Symfony project root directory in your services with a bind
Published on 2023-09-28 • Modified on 2023-09-28
This snippet shows how to inject the Symfony project root directory in your services (or your controllers) with a bind. In a previous snippet, we saw various ways to get the Symfony project's root directory. But as of Symfony 3.3, the good practice is injecting this parameter with a bind in the services.yaml
configuration file, as shown below.
services:
_defaults:
bind:
string $projectDir: '%kernel.project_dir%'
# // then inject in your services with:
# public class myService
# {
# public function __construct(string $projectDir)
# {
More on Stackoverflow Read the doc More on the web Random snippet
Call to action
Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
- Report any error/typo.
- Report something that could be improved.
- Like and repost!
- Follow me on Bluesky 🦋
- Subscribe to the RSS feed.
- Click on the More on Stackoverflow buttons to make me win "Announcer" badges 🏅.
Thank you for reading! And see you soon on Strangebuzz! 😉
