Skip to content

Commit 556b108

Browse files
committed
minor #18192 [Routing] Changing RouterInterface => UrlGeneratorInterface (ThomasLandauer)
This PR was submitted for the 6.2 branch but it was merged into the 5.4 branch instead. Discussion ---------- [Routing] Changing `RouterInterface` => `UrlGeneratorInterface` Further up on the page it's advised to use `UrlGeneratorInterface` as typehint. I don't know what the difference is, but I'm guessing that it saves you from `use RouterInterface` (if you need the `UrlGeneratorInterface` for something else anyway, as in this example). Is this right? Commits ------- 1e9f451 Chaning `RouterInterface` => `UrlGeneratorInterface`
2 parents a4d1d0f + 1e9f451 commit 556b108

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

routing.rst

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,37 +2772,32 @@ Now you'll get the expected results when generating URLs in your commands::
27722772
use Symfony\Component\Console\Input\InputInterface;
27732773
use Symfony\Component\Console\Output\OutputInterface;
27742774
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2775-
use Symfony\Component\Routing\RouterInterface;
27762775
// ...
27772776

27782777
class SomeCommand extends Command
27792778
{
2780-
private $router;
2781-
2782-
public function __construct(RouterInterface $router)
2779+
public function __construct(private UrlGeneratorInterface $urlGenerator)
27832780
{
27842781
parent::__construct();
2785-
2786-
$this->router = $router;
27872782
}
27882783

27892784
protected function execute(InputInterface $input, OutputInterface $output): int
27902785
{
27912786
// generate a URL with no route arguments
2792-
$signUpPage = $this->router->generate('sign_up');
2787+
$signUpPage = $this->urlGenerator->generate('sign_up');
27932788

27942789
// generate a URL with route arguments
2795-
$userProfilePage = $this->router->generate('user_profile', [
2790+
$userProfilePage = $this->urlGenerator->generate('user_profile', [
27962791
'username' => $user->getUserIdentifier(),
27972792
]);
27982793

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);
28022797

28032798
// when a route is localized, Symfony uses by default the current request locale
28042799
// 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']);
28062801

28072802
// ...
28082803
}

0 commit comments

Comments
 (0)