I've followed the steps from http://docs.sylius.org/en/latest/cookbook/disabling-localised-urls.html but now the URLs are generated with the _locale passed in the query string (?locale=en_US). Is there a way to disable that?
I'm looking for the same. Don't like the idea of adjusting all link generation to avoid this..
@pjedrzejewski @pamil If you could point me on how to fix this, I could make a beginning with it (if I get around the issue). I need this in my current project (high prio in the meanwhile).
@pamil Any ideas?
Can confirm the same issue and would like to remove the _locale param from the URL.
I fixed it in my project by overriding the router and adjusting the generated URL to remove the _locale parameter. As I'm using the ChainRouter, it doesn't apply directly to Sylius out of the box. Hoping to find some time next week to either submit a PR to the docs, or a PR to the core.
Hoping this gets fixed soon.
@pamil Is this something interesting to get inserted into the core? The ChainRouter option? Or just in docs? I'm using Sylius combined with LakionCms, so already have all the ChainRouting stuff included in my project, not sure what is the best solution in case LakionCms is not there.
@stefandoorn Your solution sounds good although it feels like a lot of effort to remove this parameter, I think we should have something in core - that being said, not sure if putting the chain router here makes sense. Can you make a quick gist with your solution perhaps?
Yes, it felt the same when implementing this, but it works :-) It felt more robust than overriding all the templates. Basically we need somewhere to override the url generator, which can be done by decorating the default router with a custom router and only adjust the url generator part. Basically it's not that much code or that hard.
@pamil shared a gist with me on Slack on which I built this. I can share mine later, but not right now. @pamil would you be able to share yours? My implementation is nearly the same.
That's a decorator for router:
<?php
namespace ProofOfConcept;
use Sylius\Component\Locale\Context\LocaleContextInterface;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouterInterface;
final class LocaleStrippingRouter implements RouterInterface, WarmableInterface
{
/**
* @var RouterInterface
*/
private $router;
/**
* @var LocaleContextInterface
*/
private $localeContext;
/**
* @param RouterInterface $router
* @param LocaleContextInterface $localeContext
*/
public function __construct(RouterInterface $router, LocaleContextInterface $localeContext)
{
$this->router = $router;
$this->localeContext = $localeContext;
}
/**
* {@inheritdoc}
*/
public function match($pathinfo)
{
return $this->router->match($pathinfo);
}
/**
* {@inheritdoc}
*/
public function generate($name, $parameters = [], $absolute = UrlGeneratorInterface::ABSOLUTE_PATH)
{
$url = $this->router->generate($name, $parameters, $absolute);
if (array_key_exists('_locale', $parameters) || false === strpos($url, '_locale')) {
return $url;
}
return $this->removeUnusedQueryArgument($url, '_locale', $this->localeContext->getLocaleCode());
}
/**
* {@inheritdoc}
*/
public function setContext(RequestContext $context)
{
$this->router->setContext($context);
}
/**
* {@inheritdoc}
*/
public function getContext()
{
return $this->router->getContext();
}
/**
* {@inheritdoc}
*/
public function getRouteCollection()
{
return $this->router->getRouteCollection();
}
/**
* {@inheritdoc}
*/
public function warmUp($cacheDir)
{
if ($this->router instanceof WarmableInterface) {
$this->router->warmUp($cacheDir);
}
}
/**
* @param string $url
* @param string $key
* @param string $value
*
* @return string
*/
private function removeUnusedQueryArgument($url, $key, $value)
{
$replace = [
sprintf('&%s=%s', $key, $value) => '',
sprintf('?%s=%s&', $key, $value) => '?',
sprintf('?%s=%s', $key, $value) => '',
];
return str_replace(array_keys($replace), array_values($replace), $url);
}
}
And it needs to decorate router service (and some tests as well :tada:).
@stefandoorn it strips always except when _locale parameter is explicitly passed.
I don't have much time now to both test and document it so I hope that someone who finds it valuable will help with those :)
I know, but we need a setting whether it should strip or not (some people want it in the URL's, some don't). Because from a lot of templates it is passed but we want to remove it then, so that check needs some adjustments.
http://docs.sylius.org/en/latest/cookbook/disabling-localised-urls.html
So probably a parameter or something will do: sylius.routing.disable_locale (or something) with a default value of true?
I can help a bit, but not sure what is the best way to test this. Do you have time to set-up failing tests for this if it should be in Behat? PHPUnit is ok for me also to add some testing.
@stefandoorn I am also using LakionCMS. Could you share your solution here please?
@pamil @pjedrzejewski Is this something we still want in core? I don't have a lot of time, but could have a look into it over the weekend maybe.
I'm on it! :)
Most helpful comment
I know, but we need a setting whether it should strip or not (some people want it in the URL's, some don't). Because from a lot of templates it is passed but we want to remove it then, so that check needs some adjustments.
http://docs.sylius.org/en/latest/cookbook/disabling-localised-urls.html
So probably a parameter or something will do:
sylius.routing.disable_locale(or something) with a default value of true?I can help a bit, but not sure what is the best way to test this. Do you have time to set-up failing tests for this if it should be in Behat? PHPUnit is ok for me also to add some testing.