Injecting the Symfony project root directory in your services with a PHP attribute
Published on 2023-09-29 • Modified on 2023-09-29
In the previous snippet, we saw how to inject the Symfony project root directory in the services with a bind. We can also use a PHP attribute. This new feature is available as of Symfony 6.1. Which one do you prefer? For this particular case, I find the bind more convenient to use.
<?php
declare(strict_types=1);
use Symfony\Component\DependencyInjection\Attribute\Autowire;
final class fooBarService
{
public function __construct(
#[Autowire('%kernel.project_dir%')]
private $projectDir
) {}
}
More on Stackoverflow Read the doc 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! 😉