缺少一些必需参数(“id”)来生成路由Symfony2的URL

时间:2021-03-28 10:59:38

My 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

I whant after login redirect user to his profile, but i don't know how create the controller. Now, my controller look like

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

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

I can not figure out how to do the right thing and easy. Please, reply

我无法弄清楚如何做正确的事情和轻松。请回复

1 个解决方案

#1


The $id parameter is not used in your function, you can remove it:

$ id参数未在您的函数中使用,您可以将其删除:

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

And the controller needs to return a Response, render a Twig template or use the @Template annotation:

控制器需要返回Response,呈现Twig模板或使用@Template注释:

Example with render()ing of a Twig template:

使用Twig模板的render()示例:

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

You have to create a file in src/Acme/WebsiteBundle/Resources/views/Default/profile.html.twig with the following content:

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

{{ user.firstName }}

It implies that your User entity has a getFirstName() method, it will display the first name of the user.

这意味着您的User实体具有getFirstName()方法,它将显示该用户的名字。

See the Symfony2 official documentation for template inheritance and more.

有关模板继承的信息,请参阅Symfony2官方文档。

#1


The $id parameter is not used in your function, you can remove it:

$ id参数未在您的函数中使用,您可以将其删除:

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

And the controller needs to return a Response, render a Twig template or use the @Template annotation:

控制器需要返回Response,呈现Twig模板或使用@Template注释:

Example with render()ing of a Twig template:

使用Twig模板的render()示例:

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

You have to create a file in src/Acme/WebsiteBundle/Resources/views/Default/profile.html.twig with the following content:

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

{{ user.firstName }}

It implies that your User entity has a getFirstName() method, it will display the first name of the user.

这意味着您的User实体具有getFirstName()方法,它将显示该用户的名字。

See the Symfony2 official documentation for template inheritance and more.

有关模板继承的信息,请参阅Symfony2官方文档。