dpjjr42626 2014-05-16 15:09
浏览 88
已采纳

Symfony - 如何在控制器中生成带参数的url

I want to generate a url like ../profile/firstname.lastname

But i don't understand how i can do this because i want in my php also the id from the user.

this is my controller:

       $controllers
            ->match('profile/{firstname.lastname}/', array($this, 'profile'))
            ->assert('userId', '\d+')
            ->method('GET|POST')
            ->before(array($this, 'checkLogin'))
            ->bind('home.profile');
  • 写回答

2条回答 默认 最新

  • douwei7501 2014-05-17 07:40
    关注

    You have to build your Route like this

    <route id="profile" path="/profile/{first_name}.{last_name}">
            <default key="_controller">...</default>
        </route>
    

    And in controller create like this

    $this->generateUrl('profile', [ 'first_name' => 'skowron', 'last_name' => 'line' ]);

    but if you have 2 profiles with this same data it will give you bad results. Then you can create slug or add id parameter to your route

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大