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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line