From 6e120e4cac5e2b438bbc0f1f82c52fd2ab88c0ee Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 3 Oct 2012 11:07:39 +0200 Subject: [PATCH 1/2] Add cookbook entry about generating urls in console commands --- cookbook/console/generating_urls.rst | 73 ++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 cookbook/console/generating_urls.rst diff --git a/cookbook/console/generating_urls.rst b/cookbook/console/generating_urls.rst new file mode 100644 index 00000000000..e22f38034b0 --- /dev/null +++ b/cookbook/console/generating_urls.rst @@ -0,0 +1,73 @@ +.. index:: + single: Console; Generating URLs + +How to generate URLs with a custom host in Console Commands +=========================================================== + +The command line context does not know about your VirtualHost or domain name, +therefore if you generate absolute URLs within a Console Command you generally +end up with something like ``http://localhost/foo/bar`` which is not very +useful. + +There are two ways of configuring the request context, at the application level +and per Command. + +Configuring the Request Context globally +---------------------------------------- + +To configure the Request Context - which is used by the URL Generator - you can +redefine the parameters it uses as default value to change the default host and +scheme. Note that this does not impact URL generated via normal web requests, +since those will override the defaults. + +.. configuration-block:: + + .. code-block:: yaml + + # app/config/parameters.yml + parameters: + router.request_context.host: example.org + router.request_context.scheme: https + + .. code-block:: xml + + + + + + + + + example.org + https + + + + .. code-block:: php + + // app/config/config_test.php + $container->setParameter('router.request_context.host', 'example.org'); + $container->setParameter('router.request_context.scheme', 'https'); + +Configuring the Request Context per Command +------------------------------------------- + +To change it only in one command you can simply fetch the Request Context +service and override its settings: + +.. code-block:: php + + // src/Acme/DemoBundle/Command/DemoCommand.php + class DemoCommand extends ContainerAwareCommand + { + protected function execute(InputInterface $input, OutputInterface $output) + { + $context = $this->getContainer()->get('router')->getContext(); + $context->setHost('example.com'); + $context->setScheme('https'); + + // your code here + } + } + From 77333ea31d41b8336076193f7d4e861c6958657b Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 3 Oct 2012 11:48:16 +0200 Subject: [PATCH 2/2] Clarifications --- cookbook/console/generating_urls.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cookbook/console/generating_urls.rst b/cookbook/console/generating_urls.rst index e22f38034b0..ead487eca88 100644 --- a/cookbook/console/generating_urls.rst +++ b/cookbook/console/generating_urls.rst @@ -16,9 +16,9 @@ Configuring the Request Context globally ---------------------------------------- To configure the Request Context - which is used by the URL Generator - you can -redefine the parameters it uses as default value to change the default host and -scheme. Note that this does not impact URL generated via normal web requests, -since those will override the defaults. +redefine the parameters it uses as default values to change the default host +(localhost) and scheme (http). Note that this does not impact URLs generated +via normal web requests, since those will override the defaults. .. configuration-block::