Router generates absolute URL with localhost
Edit on GitHubRouter generates URLs with an absolute path instead of a relative one.
Cause
Prior to version 1.9.0 of the Router module, Spryker did not set the RequestContext
properly in ChainRouter
. This led to an issue that URLs were generated with an absolute path instead of a relative one. This silently happens inside Symfony’s router when the host retrieved from RequestContext
doesn’t match the one that the resolved route has.
Solution
Do the following:
- Update the Router module to at least version 1.9.0.
- Set
RequestContext
as follows: Extend\Spryker\Yves\Router\Router\ChainRouter
in your project, add a newaddRequestContext()
method to it and call that method in the constructor of that class.
/**
* @param \Symfony\Component\Routing\RequestContext|null $requestContext
*
* @return void
*/
protected function addRequestContext(?RequestContext $requestContext = null): void
{
$request = Request::createFromGlobals();
if (!$requestContext) {
$requestContext = new RequestContext();
}
$requestContext->fromRequest($request);
$this->setContext($requestContext);
}
- Update the constructor:
/**
* @param \Spryker\Yves\RouterExtension\Dependency\Plugin\RouterPluginInterface[] $routerPlugins
* @param \Psr\Log\LoggerInterface|null $logger
* @param \Symfony\Component\Routing\RequestContext|null $requestContext
*/
public function __construct(array $routerPlugins, ?LoggerInterface $logger = null, ?RequestContext $requestContext = null)
{
parent::__construct($logger);
$this->addRequestContext($requestContext);
$this->addRouterPlugins($routerPlugins);
}
Thank you!
For submitting the form