简体   繁体   English

Symfony Url Generator在登录之前和之后从同一Route发出不同的URL

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

I currently using Symfony Kernel and Routing within a custom framework and I have run into a curious problem. 我目前在自定义框架中使用Symfony内核和路由,我遇到了一个奇怪的问题。 Using the same Route: 使用相同的路线:

$collection->add('article_edit', new Route('/articles/edit/{alias}', array(
    '_controller' => 'AppBundle:Article:edit'
)));

Using this Url Generator code 使用此Url Generator代码

$url = $this->generateUrl('article_edit',array('alias' => 'test'));

Before login, the Url Generator produces 登录前,Url Generator会生成

/articles/edit/test /文章/编辑/测试

And after login, it produces 登录后,它会产生

http://localhost/testsite/articles/edit/test HTTP://本地主机/测试网站/用品/编辑/测试

Without anything being changed, what could be happening here since I want the same result consistently. 没有任何改变,这里可能会发生什么,因为我一直想要相同的结果。

Thanks 谢谢

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. 我可以看到“/ articles / edit / test”和“ http:// localhost / testsite / articles / edit / test ”是相同的网址,只是网站网址不同。

if you use your "generateUrl" method as following it will generate always same url. 如果您使用“generateUrl”方法,它将生成始终相同的URL。 It should generate " http://localhost/testsite/articles/edit/test " always 它应该始终生成“ http:// localhost / testsite / articles / edit / test

$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 尝试在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 更多信息: https//symfony.com/doc/3.3/console/request_context.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM