@@ -21,8 +21,8 @@ Configuring the Request Context Globally
21
21
22
22
To configure the Request Context - which is used by the URL Generator - you can
23
23
redefine 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.
26
26
27
27
Note that this does not impact URLs generated via normal web requests, since those
28
28
will override the defaults.
@@ -31,15 +31,15 @@ will override the defaults.
31
31
32
32
.. code-block :: yaml
33
33
34
- # app/ config/parameters.yml
34
+ # config/services.yaml
35
35
parameters :
36
36
router.request_context.host : example.org
37
37
router.request_context.scheme : https
38
38
router.request_context.base_url : my/path
39
39
40
40
.. code-block :: xml
41
41
42
- <!-- app/ config/parameters .xml -->
42
+ <!-- config/services .xml -->
43
43
<?xml version =" 1.0" encoding =" UTF-8" ?>
44
44
<container xmlns =" http://symfony.com/schema/dic/services"
45
45
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance" >
@@ -54,29 +54,37 @@ will override the defaults.
54
54
55
55
.. code-block :: php
56
56
57
- // app/ config/parameters .php
57
+ // config/services .php
58
58
$container->setParameter('router.request_context.host', 'example.org');
59
59
$container->setParameter('router.request_context.scheme', 'https');
60
60
$container->setParameter('router.request_context.base_url', 'my/path');
61
61
62
62
Configuring the Request Context per Command
63
63
-------------------------------------------
64
64
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::
67
67
68
- // src/Command/DemoCommand.php
68
+ // src/Command/DemoCommand.php
69
+ use Symfony\Component\Routing\RouterInterface;
70
+ // ...
69
71
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;
79
75
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