Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Url Generator producing different URLs from the same Route before and after login

Tags:

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

like image 810
Stanley Ngumo Avatar asked Jan 02 '17 07:01

Stanley Ngumo


People also ask

When does Symfony use the same locale for multiple URLs?

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.

When should I take control of Symfony route names?

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.

How does routing work in Symfony?

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 ).

How does Symfony redirect between URLs without trailing slashes?

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.


2 Answers

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; 
like image 130
hanish singla Avatar answered Sep 19 '22 13:09

hanish singla


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

like image 43
Christoph Avatar answered Sep 20 '22 13:09

Christoph