Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

php - Symfony 5 Generating a URL from Console Command

I'm new to Symfony and am using 5.x. I have created a Console command using SymfonyComponentConsoleCommandCommand and am trying to use SymfonyComponentHttpClientHttpClient 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 SymfonyComponentRoutingGeneratorUrlGeneratorInterface, and the web is cluttered with code samples for old versions of Symfony, and I haven't yet managed to get this working.

My latest attempt was:

public function __construct(UrlGeneratorInterface $router)
{
    parent::__construct();
    $this->router = $router;
}

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.

$url = $context->generate('view', ['Param' => $message['Param']], UrlGeneratorInterface::ABSOLUTE_URL);

services.yaml:

AppCommandMyCommand:
    arguments: ['@router.default']
  1. Is there a simpler way to generate a URL from a Console Command by explicitly specifying host, protocol, port, route, parameters etc?
  2. Why isn't UrlGeneratorInterface or RouterInterface autowiring?
  3. Do I need to specify wiring manually as $router.default in services.yaml if I also have autowiring enabled?
  4. 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.

EDIT: Updated gist: https://gist.github.com/tSixTM/86a29ee75dbd117c8f8571d458ed72db

EDIT 2: Made the problem statement clearer by adding question points: I slept on it :)

EDIT 3:

#!/usr/bin/env php
<?php
// application.php

require __DIR__.'/vendor/autoload.php';

use SymfonyComponentConsoleApplication;

$application = new Application();

$application->add(new AppCommandMyCommand());

$application->run();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I tinkered around with your gist and found the following to work: https://gist.github.com/Matts/528c249a82e5844164039c4f6c0db046

The problem that you seemed to have, was not due to your service declaration, rather it was that you were missing the declaration of the private $router variable in MyCommand, see line 25.

So you can keep the services.yaml as you show in your gist, no changes required to the autowire variable, also you don't have to manually declare the command

Further, you don't need to fetch $context from the router, you can also set the base URL in your framework.yaml, here you can find where I found this.

Please note that I removed some code from the execute, this was due to me not having access to your other files. You can just re-add this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...