【问题标题】:Some mandatory parameters are missing ("id") to generate a URL for route Symfony2缺少一些强制参数(“id”)来生成路由 Symfony2 的 URL
【发布时间】:2015-05-03 22:26:40
【问题描述】:

我的 security.yml

firewalls:
    user_area:
        pattern: ^/
        anonymous: ~
        provider: chain_provider
        form_login:
            login_path: login_action
            check_path: login_check
            csrf_provider: form.csrf_provider
            default_target_path: user_show_redirect
        logout:
            path: logout_action
            target: /login

我想在登录后将用户重定向到他的个人资料,但我不知道如何创建控制器。现在,我的控制器看起来像

 /**
 * @Route("/user/show", name="user_show_redirect")
 * @return array
 */
public function redirectAction($id)
{
    return array('user' => $this->getUser());
}

我无法弄清楚如何做正确且容易的事情。请回复

【问题讨论】:

  • 尝试使用getUser()在控制器中获取当前用户,而不是在URL参数中。
  • 你能给我写个小例子吗...
  • 我看错了问题,问题出在id,而不是getUser()
  • 请避免编辑您的问题以删除原始问题,现在内容与标题不一致。
  • 在你的问题末尾添加新的错误信息,不要改变原来的问题。

标签: symfony


【解决方案1】:

你的函数中没有使用$id参数,你可以去掉它:

/**
 * @Route("/user/show", name="user_show_redirect")
 * @return array
 */
public function redirectAction()
{
    return array('user' => $this->getUser());
}

并且控制器需要返回一个Response,渲染一个Twig模板或者使用@Template annotation

render()ing of a Twig template 为例:

/**
 * @Route("/user/show", name="user_show_redirect")
 * @return array
 */
public function redirectAction()
{
    return $this->render(
        'AcmeWebsiteBundle:Default:profile.html.twig',
        array('user' => $this->getUser())
    );
}

您必须在 src/Acme/WebsiteBundle/Resources/views/Default/profile.html.twig 中创建一个文件,其内容如下:

{{ user.firstName }}

这意味着你的User实体有一个getFirstName()方法,它会显示用户的名字。

请参阅Symfony2 official documentation 了解模板继承等信息。

【讨论】:

  • 我收到错误The controller must return a response (Array(user => Object(Qualium\ModelBundle\Entity\User)) given).
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多