【问题标题】:Symfony parameters Routes (Easy to answer)Symfony 参数路由(容易回答)
【发布时间】:2018-12-28 11:55:47
【问题描述】:

我尝试在参数中传递我的用户 ID,因为我想在其他函数中使用它来链接它们,但不知道为什么这不起作用(我认为我做错了,很容易回答谢谢 :)

return $this->redirect('/profile/new/', array(
            'id' => $user->getId(),
        ));

我的接收器:

/**
 * Creates a new profile entity.
 *
 * @Route("/new/{id}", name="profile_new")
 */
public function newProfileAction(Request $request)
{

例如,当我执行/profile/new/8 时,它可以工作!但是当我点击按钮提交时,不使用 id 重定向......(当然路线很好,当我这样做时 - 它有效:

return $this->redirect('/profile/new');

我的接收器(当它工作时):

/**
 * Creates a new profile entity.
 *
 * @Route("/new", name="profile_new")
 */
public function newProfileAction(Request $request)
{

【问题讨论】:

  • 你能添加更多代码吗?
  • 你需要什么?

标签: symfony redirect parameters routes


【解决方案1】:

你应该使用 $this->redirectToRoute('ROUTENAME',[PARAMETERS]) 表示:

$this->redirectToRoute('profile_new',['id'=>$ID])

如果你使用 $this->redirect('URL') 你必须解析 URL 所以你需要 "/profile/new/ID"

【讨论】:

  • thx so mutch :) 那么 /{id} 和 /ID 有什么区别?
  • @LuisRosario /ID 是固定页面,/{id} 是动态页面,其中包含您提供的变量。
  • 所以如果我想传递所有用户并为用户修改另一个函数中的变量我该怎么办?
  • 如果我想在配置文件功能中做:$user->setIdProfile($profile->getId());
  • 在带有 url 的选项中,您必须自己生成 url 字符串。使用 toRoute 选项 symfony 将为您生成真实的 url。我不建议你自己生成url,让symfony为你做吧!
猜你喜欢
  • 1970-01-01
  • 2016-06-24
  • 1970-01-01
  • 2011-03-05
  • 2016-02-06
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 2020-04-20
相关资源
最近更新 更多