@@ -2031,49 +2031,57 @@ Generating URLs in Commands
20312031
20322032Generating URLs in commands works the same as
20332033:ref: `generating URLs in services <routing-generating-urls-in-services >`. The
2034- only difference is that commands are not executed in the HTTP context, so they
2035- don't have access to HTTP requests. In practice, this means that if you generate
2036- absolute URLs, you'll get ``http://localhost/ `` as the host name instead of your
2037- real host name.
2034+ only difference is that commands are not executed in the HTTP context. Therefore,
2035+ if you generate absolute URLs, you'll get ``http://localhost/ `` as the host name
2036+ instead of your real host name.
20382037
2039- The solution is to configure the "request context" used by commands when they
2040- generate URLs. This context can be configured globally for all commands :
2038+ The solution is to configure the `` default_uri `` option to define the
2039+ "request context" used by commands when they generate URLs :
20412040
20422041.. configuration-block ::
20432042
20442043 .. code-block :: yaml
20452044
2046- # config/services .yaml
2047- parameters :
2048- router.request_context.host : ' example.org '
2049- router.request_context.base_url : ' my/path '
2050- asset.request_context.base_path : ' %router.request_context.base_url% '
2045+ # config/packages/routing .yaml
2046+ framework :
2047+ router :
2048+ # ...
2049+ default_uri : ' https://example.org/my/path/ '
20512050
20522051 .. code-block :: xml
20532052
2054- <!-- config/services .xml -->
2055- <?xml version =" 1.0" encoding =" UTF-8" ?>
2053+ <!-- config/packages/routing .xml -->
2054+ <?xml version =" 1.0" encoding =" UTF-8" ?>
20562055 <container xmlns =" http://symfony.com/schema/dic/services"
20572056 xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2057+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
20582058 xsi : schemaLocation =" http://symfony.com/schema/dic/services
2059- https://symfony.com/schema/dic/services/services-1.0.xsd" >
2060-
2061- <parameters >
2062- <parameter key =" router.request_context.host" >example.org</parameter >
2063- <parameter key =" router.request_context.base_url" >my/path</parameter >
2064- <parameter key =" asset.request_context.base_path" >%router.request_context.base_url%</parameter >
2065- </parameters >
2059+ https://symfony.com/schema/dic/services/services-1.0.xsd
2060+ http://symfony.com/schema/dic/symfony
2061+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
20662062
2063+ <framework : config >
2064+ <framework : router default-uri =" https://example.org/my/path/" >
2065+ <!-- ... -->
2066+ </framework : router >
2067+ </framework : config >
20672068 </container >
20682069
20692070 .. code-block :: php
20702071
2071- // config/services.php
2072- $container->setParameter('router.request_context.host', 'example.org');
2073- $container->setParameter('router.request_context.base_url', 'my/path');
2074- $container->setParameter('asset.request_context.base_path', $container->getParameter('router.request_context.base_url'));
2072+ // config/packages/routing.php
2073+ $container->loadFromExtension('framework', [
2074+ 'router' => [
2075+ // ...
2076+ 'default_uri' => "https://example.org/my/path/",
2077+ ],
2078+ ]);
2079+
2080+ .. versionadded :: 5.1
2081+
2082+ The ``default_uri `` option was introduced in Symfony 5.1.
20752083
2076- This information can be configured per command too ::
2084+ Now you'll get the expected results when generating URLs in your commands ::
20772085
20782086 // src/Command/SomeCommand.php
20792087 namespace App\Command;
@@ -2098,11 +2106,6 @@ This information can be configured per command too::
20982106
20992107 protected function execute(InputInterface $input, OutputInterface $output)
21002108 {
2101- // these values override any global configuration
2102- $context = $this->router->getContext();
2103- $context->setHost('example.com');
2104- $context->setBaseUrl('my/path');
2105-
21062109 // generate a URL with no route arguments
21072110 $signUpPage = $this->router->generate('sign_up');
21082111
@@ -2123,6 +2126,12 @@ This information can be configured per command too::
21232126 }
21242127 }
21252128
2129+ .. note ::
2130+
2131+ By default, the URLs generated for web assets use the same ``default_uri ``
2132+ value, but you can change it with the ``asset.request_context.base_path ``
2133+ and ``asset.request_context.secure `` container parameters.
2134+
21262135Checking if a Route Exists
21272136~~~~~~~~~~~~~~~~~~~~~~~~~~
21282137
0 commit comments