title: Symfony 5 Generating a URL from Console Command
body: <p>I'm new to Symfony and am using 5.x. I have created a Console command using Symfony\Component\Console\Command\Command and am trying to use Symfony\Component\HttpClient\HttpClient to POST to a URL. I need to generate the URL to a route running on the same machine (but in future this may possibly change to a different machine), so the host could be like localhost or example.com, and the port of the API is custom. I have searched on the web but the only possible solution I got involved the use of Symfony\Component\Routing\Generator\UrlGeneratorInterface, and the web is cluttered with code samples for old versions of Symfony, and I haven't yet managed to get this working.</p>
  
  <p>My latest attempt was:</p>
  
  <pre><code>public function __construct(UrlGeneratorInterface $router)
  {
      parent::__construct();
      $this-&gt;router = $router;
  }
  </code></pre>
  
  <p>but I don't really understand how to inject the parameter UrlGeneratorInterface $router to the constructor. I get an error that the parameter was not supplied. Do I have to create an instance of UrlGenerator elsewhere and inject it over here, or is there a simpler way to just generate an absolute URL in Symfony from within a Command? I don't really understand containers yet.</p>
  
  <pre><code>$url = $context-&gt;generate('view', ['Param' =&gt; $message['Param']], UrlGeneratorInterface::ABSOLUTE_URL);
  </code></pre>
  
  <p>services.yaml:</p>
  
  <pre><code>App\Command\MyCommand:
      arguments: ['@router.default']
  </code></pre>
  
  <ol>
  <li>Is there a simpler way to generate a URL from a Console Command by
  explicitly specifying host, protocol, port, route, parameters etc? </li>
  <li>Why isn't UrlGeneratorInterface or RouterInterface autowiring?</li>
  <li>Do I need to specify wiring manually as $router.default in
  services.yaml if I also have autowiring enabled?</li>
  <li>I understand that the execute function implementation may be
  incorrect, but I couldn't get to fixing that without first getting
  the constructor working. This is still, work in progress.</li>
  </ol>
  
  <p>EDIT:
  Updated gist: <a href="https://gist.github.com/tSixTM/86a29ee75dbd117c8f8571d458ed72db" rel="nofollow noreferrer">https://gist.github.com/tSixTM/86a29ee75dbd117c8f8571d458ed72db</a></p>
  
  <p>EDIT 2: Made the problem statement clearer by adding question points: I slept on it :)</p>
  
  <p>EDIT 3:</p>
  
  <pre><code>#!/usr/bin/env php
  &lt;?php
  // application.php
  
  require __DIR__.'/vendor/autoload.php';
  
  use Symfony\Component\Console\Application;
  
  $application = new Application();
  
  $application-&gt;add(new App\Command\MyCommand());
  
  $application-&gt;run();
  </code></pre>