@@ -2772,37 +2772,32 @@ Now you'll get the expected results when generating URLs in your commands::
2772
2772
use Symfony\Component\Console\Input\InputInterface;
2773
2773
use Symfony\Component\Console\Output\OutputInterface;
2774
2774
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2775
- use Symfony\Component\Routing\RouterInterface;
2776
2775
// ...
2777
2776
2778
2777
class SomeCommand extends Command
2779
2778
{
2780
- private $router;
2781
-
2782
- public function __construct(RouterInterface $router)
2779
+ public function __construct(private UrlGeneratorInterface $urlGenerator)
2783
2780
{
2784
2781
parent::__construct();
2785
-
2786
- $this->router = $router;
2787
2782
}
2788
2783
2789
2784
protected function execute(InputInterface $input, OutputInterface $output): int
2790
2785
{
2791
2786
// generate a URL with no route arguments
2792
- $signUpPage = $this->router ->generate('sign_up');
2787
+ $signUpPage = $this->urlGenerator ->generate('sign_up');
2793
2788
2794
2789
// generate a URL with route arguments
2795
- $userProfilePage = $this->router ->generate('user_profile', [
2790
+ $userProfilePage = $this->urlGenerator ->generate('user_profile', [
2796
2791
'username' => $user->getUserIdentifier(),
2797
2792
]);
2798
2793
2799
- // generated URLs are "absolute paths" by default . Pass a third optional
2800
- // argument to generate different URLs (e.g. an "absolute URL")
2801
- $signUpPage = $this->router ->generate('sign_up', [], UrlGeneratorInterface::ABSOLUTE_URL);
2794
+ // by default, generated URLs are "absolute paths". Pass a third optional
2795
+ // argument to generate different URIs (e.g. an "absolute URL")
2796
+ $signUpPage = $this->urlGenerator ->generate('sign_up', [], UrlGeneratorInterface::ABSOLUTE_URL);
2802
2797
2803
2798
// when a route is localized, Symfony uses by default the current request locale
2804
2799
// pass a different '_locale' value if you want to set the locale explicitly
2805
- $signUpPageInDutch = $this->router ->generate('sign_up', ['_locale' => 'nl']);
2800
+ $signUpPageInDutch = $this->urlGenerator ->generate('sign_up', ['_locale' => 'nl']);
2806
2801
2807
2802
// ...
2808
2803
}
0 commit comments