24

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

9
  • The difference I see is url turning to ABSOLUTE_URL after login. However, the default is ABSOLUTE_PATH. Try $url = $this->generateUrl('article_edit',array('alias' => 'test'), 0); See what it returns it for you even after login.
    – Jeet
    Jan 3, 2017 at 7:06
  • @Jeet - Even when I change the url type to ABSOLUTE_PATH the url still generates different URLs. In fact it also changes when I submit a form i.e. use $_POST variables. Jan 7, 2017 at 21:08
  • Please post the headers you send before and after login.
    – Don Omondi
    Jan 15, 2017 at 1:56
  • 2
    Can you please add your routing and security configuration settings? Jan 16, 2017 at 15:49
  • 1
    /articles/edit/test is somehow relative. It will use you current environment via proper app*.php file. After login you route is different - http://localhost/testsite/articles/edit/test - maybe you are using production environment with old/cached routes compiled? Try clearing your cache and make sure you don't switch environments.
    – broiniac
    Jun 11, 2017 at 20:32

2 Answers 2

2

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;
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.