Skip to content

Add cookbook entry about generating urls in console commands #1778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions cookbook/console/generating_urls.rst
Original file line number Diff line number Diff line change
@@ -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 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::

.. code-block:: yaml

# app/config/parameters.yml
parameters:
router.request_context.host: example.org
router.request_context.scheme: https

.. code-block:: xml

<!-- app/config/parameters.xml -->

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line should be removed.

<?xml version="1.0" encoding="UTF-8"?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<parameters>
<parameter key="router.request_context.host">example.org</parameter>
<parameter key="router.request_context.scheme">https</parameter>
</parameters>
</container>

.. 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the :: shorthand here (remove this line and the line before and put an extra colon at the end of the sentence)


// src/Acme/DemoBundle/Command/DemoCommand.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place // ... after this line, because you folded the use statements) and put an empty line between the 2 comments.

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be // .. your code here

}
}