@@ -2031,49 +2031,57 @@ Generating URLs in Commands
2031
2031
2032
2032
Generating URLs in commands works the same as
2033
2033
: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.
2038
2037
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 :
2041
2040
2042
2041
.. configuration-block ::
2043
2042
2044
2043
.. code-block :: yaml
2045
2044
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/ '
2051
2050
2052
2051
.. code-block :: xml
2053
2052
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" ?>
2056
2055
<container xmlns =" http://symfony.com/schema/dic/services"
2057
2056
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2057
+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
2058
2058
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" >
2066
2062
2063
+ <framework : config >
2064
+ <framework : router default-uri =" https://example.org/my/path/" >
2065
+ <!-- ... -->
2066
+ </framework : router >
2067
+ </framework : config >
2067
2068
</container >
2068
2069
2069
2070
.. code-block :: php
2070
2071
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.
2075
2083
2076
- This information can be configured per command too ::
2084
+ Now you'll get the expected results when generating URLs in your commands ::
2077
2085
2078
2086
// src/Command/SomeCommand.php
2079
2087
namespace App\Command;
@@ -2098,11 +2106,6 @@ This information can be configured per command too::
2098
2106
2099
2107
protected function execute(InputInterface $input, OutputInterface $output)
2100
2108
{
2101
- // these values override any global configuration
2102
- $context = $this->router->getContext();
2103
- $context->setHost('example.com');
2104
- $context->setBaseUrl('my/path');
2105
-
2106
2109
// generate a URL with no route arguments
2107
2110
$signUpPage = $this->router->generate('sign_up');
2108
2111
@@ -2123,6 +2126,12 @@ This information can be configured per command too::
2123
2126
}
2124
2127
}
2125
2128
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
+
2126
2135
Checking if a Route Exists
2127
2136
~~~~~~~~~~~~~~~~~~~~~~~~~~
2128
2137
0 commit comments