コマンドを使用して絶対 URL を生成するための追加情報 (たとえば、電子メールを送信するため)
コマンドで{{ absolute_url(path('index')) }}
は、そのままでは機能しません。
antongorodezkiy の回答に示されている追加の構成を追加する必要があります。
ただし、アプリ全体にどのように影響するかわからないために構成を変更したくない場合は、コマンドでルーターを構成できます。
ドキュメントは次のとおりです。
https://symfony.com/doc/3.4/console/request_context.html
コードは次のとおりです。
use Symfony\Component\Routing\RouterInterface;
// ...
class DemoCommand extends Command
{
private $router;
public function __construct(RouterInterface $router)
{
parent::__construct();
$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');
$url = $this->router->generate('route-name', ['param-name' => 'param-value']);
// ...
}
}
Twig テンプレートで URL を生成するには
<a href="{{ absolute_url(path(...)) }}"></a>
envファイルからHOSTとSCHEMEを取得できます
$context = $this->router->getContext();
$context->setHost($_ENV['NL_HOST']);
$context->setScheme($_ENV['NL_SCHEME']);
.env および .env.local ファイルで変数を定義するだけです
NL_HOST=mydomain.com
NL_SCHEME=https