diff --git a/routing.rst b/routing.rst index 76184d7c8ef..8c767881330 100644 --- a/routing.rst +++ b/routing.rst @@ -2031,49 +2031,57 @@ Generating URLs in Commands Generating URLs in commands works the same as :ref:`generating URLs in services `. The -only difference is that commands are not executed in the HTTP context, so they -don't have access to HTTP requests. In practice, this means that if you generate -absolute URLs, you'll get ``http://localhost/`` as the host name instead of your -real host name. +only difference is that commands are not executed in the HTTP context. Therefore, +if you generate absolute URLs, you'll get ``http://localhost/`` as the host name +instead of your real host name. -The solution is to configure the "request context" used by commands when they -generate URLs. This context can be configured globally for all commands: +The solution is to configure the ``default_uri`` option to define the +"request context" used by commands when they generate URLs: .. configuration-block:: .. code-block:: yaml - # config/services.yaml - parameters: - router.request_context.host: 'example.org' - router.request_context.base_url: 'my/path' - asset.request_context.base_path: '%router.request_context.base_url%' + # config/packages/routing.yaml + framework: + router: + # ... + default_uri: 'https://example.org/my/path/' .. code-block:: xml - - + + - - - example.org - my/path - %router.request_context.base_url% - + https://symfony.com/schema/dic/services/services-1.0.xsd + http://symfony.com/schema/dic/symfony + https://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> + + + + + .. code-block:: php - // config/services.php - $container->setParameter('router.request_context.host', 'example.org'); - $container->setParameter('router.request_context.base_url', 'my/path'); - $container->setParameter('asset.request_context.base_path', $container->getParameter('router.request_context.base_url')); + // config/packages/routing.php + $container->loadFromExtension('framework', [ + 'router' => [ + // ... + 'default_uri' => "https://example.org/my/path/", + ], + ]); + +.. versionadded:: 5.1 + + The ``default_uri`` option was introduced in Symfony 5.1. -This information can be configured per command too:: +Now you'll get the expected results when generating URLs in your commands:: // src/Command/SomeCommand.php namespace App\Command; @@ -2098,11 +2106,6 @@ This information can be configured per command too:: protected function execute(InputInterface $input, OutputInterface $output) { - // these values override any global configuration - $context = $this->router->getContext(); - $context->setHost('example.com'); - $context->setBaseUrl('my/path'); - // generate a URL with no route arguments $signUpPage = $this->router->generate('sign_up'); @@ -2123,6 +2126,12 @@ This information can be configured per command too:: } } +.. note:: + + By default, the URLs generated for web assets use the same ``default_uri`` + value, but you can change it with the ``asset.request_context.base_path`` + and ``asset.request_context.secure`` container parameters. + Checking if a Route Exists ~~~~~~~~~~~~~~~~~~~~~~~~~~