Symfony 4 was released on November 30th.
Update now to the best Symfony ever!

You are browsing the Symfony 4 documentation, which changes significantly from Symfony 3.x. If your app doesn't use Symfony 4 yet, browse the Symfony 3.4 documentation.

How to Generate URLs from the Console

How to Generate URLs from the Console

Unfortunately, the command line context does not know about your VirtualHost or domain name. This means that if you generate absolute URLs within a console command you'll probably end up with something like http://localhost/foo/bar which is not very useful.

To fix this, you need to configure the "request context", which is a fancy way of saying that you need to configure your environment so that it knows what URL it should use when generating URLs.

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). You can also configure the base path if Symfony is not running in the root directory.

Note that this does not impact URLs generated via normal web requests, since those will override the defaults.

  • YAML
    1
    2
    3
    4
    5
    # config/services.yaml
    parameters:
        router.request_context.host: example.org
        router.request_context.scheme: https
        router.request_context.base_url: my/path
    
  • XML
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    <!-- config/services.xml -->
    <?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>
            <parameter key="router.request_context.base_url">my/path</parameter>
        </parameters>
    
    </container>
    
  • PHP
    1
    2
    3
    4
    // config/services.php
    $container->setParameter('router.request_context.host', 'example.org');
    $container->setParameter('router.request_context.scheme', 'https');
    $container->setParameter('router.request_context.base_url', 'my/path');
    

Configuring the Request Context per Command

To change it only in one command you can fetch the Request Context from the router service and override its settings:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// src/Command/DemoCommand.php
use Symfony\Component\Routing\RouterInterface;
// ...

class DemoCommand extends ContainerAwareCommand
{
    private $router;

    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $context = $this->router->getContext();
        $context->setHost('example.com');
        $context->setScheme('https');
        $context->setBaseUrl('my/path');

        // ... your code here
    }
}

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.