I currently using Symfony Kernel and Routing within a custom framework and I have run into a curious problem. Using the same Route:
$collection->add('article_edit', new Route('/articles/edit/{alias}', array( '_controller' => 'AppBundle:Article:edit' )));
Using this Url Generator code
$url = $this->generateUrl('article_edit',array('alias' => 'test'));
Before login, the Url Generator produces
/articles/edit/test
And after login, it produces
http://localhost/testsite/articles/edit/test
Without anything being changed, what could be happening here since I want the same result consistently.
Thanks
When a localized route is matched, Symfony uses the same locale automatically during the entire request. When the application uses full "language + territory" locales (e.g. fr_FR , fr_BE ), if the URLs are the same in all related locales, routes can use only the language part (e.g. fr) to avoid repeating the same URLs.
By default, Symfony automatically generates a name for us, which is fine. The name isn't used at all until we generate a URL to it. And as soon as we do need to generate a URL to a route, I highly recommend taking control of this name... just to make sure it never accidentally changes.
When your application receives a request, it calls a controller action to generate the response. The routing configuration defines which action to run for each incoming URL. It also provides other useful features, like generating SEO-friendly URLs (e.g. /read/intro-to-symfony instead of index.php?article_id=57 ).
Symfony follows this logic to redirect between URLs with and without trailing slashes (but only for GET and HEAD requests): Routes can configure a host option to require that the HTTP host of the incoming requests matches some specific value.
I can see that "/articles/edit/test" and "http://localhost/testsite/articles/edit/test" are same url just with difference of site url in it.
if you use your "generateUrl" method as following it will generate always same url. It should generate "http://localhost/testsite/articles/edit/test" always
$url = $this->generateUrl('article_edit',array('alias' => 'test', UrlGeneratorInterface::ABSOLUTE_URL));
Also dont forget to use following class in namespace section
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Try setting your host in app/config/services.yml
parameters: router.request_context.host: www.yourdomain.com router.request_context.scheme: http router.request_context.base_url: /your/path
More Info: https://symfony.com/doc/3.3/console/request_context.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With