@@ -21,8 +21,8 @@ Configuring the Request Context Globally
2121
2222To configure the Request Context - which is used by the URL Generator - you can
2323redefine the parameters it uses as default values to change the default host
24- (localhost) and scheme (http). You can also configure the base path if Symfony
25- is not running in the root directory.
24+ (`` localhost `` ) and scheme (`` http `` ). You can also configure the base path if
25+ Symfony is not running in the root directory.
2626
2727Note that this does not impact URLs generated via normal web requests, since those
2828will override the defaults.
@@ -31,15 +31,15 @@ will override the defaults.
3131
3232 .. code-block :: yaml
3333
34- # app/ config/parameters.yml
34+ # config/services.yaml
3535 parameters :
3636 router.request_context.host : example.org
3737 router.request_context.scheme : https
3838 router.request_context.base_url : my/path
3939
4040 .. code-block :: xml
4141
42- <!-- app/ config/parameters .xml -->
42+ <!-- config/services .xml -->
4343 <?xml version =" 1.0" encoding =" UTF-8" ?>
4444 <container xmlns =" http://symfony.com/schema/dic/services"
4545 xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" >
@@ -54,29 +54,37 @@ will override the defaults.
5454
5555 .. code-block :: php
5656
57- // app/ config/parameters .php
57+ // config/services .php
5858 $container->setParameter('router.request_context.host', 'example.org');
5959 $container->setParameter('router.request_context.scheme', 'https');
6060 $container->setParameter('router.request_context.base_url', 'my/path');
6161
6262 Configuring the Request Context per Command
6363-------------------------------------------
6464
65- To change it only in one command you can simply fetch the Request Context
66- from the `` router `` service and override its settings::
65+ To change it only in one command you can fetch the Request Context from the
66+ router service and override its settings::
6767
68- // src/Command/DemoCommand.php
68+ // src/Command/DemoCommand.php
69+ use Symfony\Component\Routing\RouterInterface;
70+ // ...
6971
70- // ...
71- class DemoCommand extends ContainerAwareCommand
72- {
73- protected function execute(InputInterface $input, OutputInterface $output)
74- {
75- $context = $this->getContainer()->get('router')->getContext();
76- $context->setHost('example.com');
77- $context->setScheme('https');
78- $context->setBaseUrl('my/path');
72+ class DemoCommand extends ContainerAwareCommand
73+ {
74+ private $router;
7975
80- // ... your code here
81- }
82- }
76+ public function __construct(RouterInterface $router)
77+ {
78+ $this->router = $router;
79+ }
80+
81+ protected function execute(InputInterface $input, OutputInterface $output)
82+ {
83+ $context = $this->router->getContext();
84+ $context->setHost('example.com');
85+ $context->setScheme('https');
86+ $context->setBaseUrl('my/path');
87+
88+ // ... your code here
89+ }
90+ }
0 commit comments