Symfony - generate url with parameter in controller

后端 未结 4 1964
花落未央
花落未央 2021-02-01 01:00

I want to generate a Url directly in my controller. I want to user a url defined in my routing.yml file that needs a parameter.

I\'ve found that code in the Cookbook (Ro

4条回答
  •  孤独总比滥情好
    2021-02-01 01:16

    If you want absolute urls, you have the third parameter.

    $product_url = $this->generateUrl('product_detail', 
        array(
            'slug' => 'slug'
        ),
        UrlGeneratorInterface::ABSOLUTE_URL
    );
    

    Remember to include UrlGeneratorInterface.

    use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
    

提交回复
热议问题