简体   繁体   English

如何在Symfony2中将变量从一个页面传递到另一个页面

[英]How to pass variables from one page to another in Symfony2

I have a login page that when logged in, returns a welcome page with the name of the user on it. 我有一个登录页面,登录后返回一个欢迎页面,上面有用户名。 I use this code to pass the variable $user to the welcome page: 我使用此代码将变量$ user传递给欢迎页面:

return $this->render('LoginLoginBundle:Default:welcome.html.twig', array('user' => $user));

Now on the welcome page I have a nav, with the link to manager.html.twig on it. 现在在欢迎页面上,我有一个导航,上面有manager.html.twig的链接。

This is the code of the link on the welcome page: 这是欢迎页面上链接的代码:

<li><a href="{{path('login_login_managerPage')}}">Manager</a></li>

The link goes to this route: 链接转到此路线:

login_login_managerPage:
path:   /managerPage
defaults: { _controller: LoginLoginBundle:Default:manager }

which points to the managerAction: 哪个指向managerAction:

public function managerAction(Request $request) {
    $session = $this->getRequest()->getSession();
    $em = $this->getDoctrine()->getEntityManager();
    $repository = $em->getRepository('LoginLoginBundle:User');
    if ($session->has('login')) {
        $login = $session->get('login');
        $username = $login->getUsername();
        $password = $login->getPassword();
        $user = $repository->findOneBy(array('username' => $username, 'password' => $password));
        if ($user) {
            return $this->render('LoginLoginBundle:Default:manager.html.twig', array('user' => $user));
        }
    }
    return $this->render('LoginLoginBundle:Default:login.html.twig');
}

Now I can't get $user to the managerpage. 现在我无法获得$ user到managerpage。 How can I pass it properly between pages? 如何在页面之间正确传递?

EDIT: I editedy code but now I get this error: 编辑:我编辑了代码,但现在我收到此错误:

Impossible to access an attribute ("username") on a NULL variable ("") in LoginLoginBundle:Default:welcome.html.twig at line 10 

Instead of writing your own security system, I would recommend you to use Symfony's own security component which has all the important things already done. 我建议您使用Symfony自己的安全组件,而不是编写自己的安全系统,该组件已经完成了所有重要的事情。 Please read carefully this link : http://symfony.com/doc/current/book/security.html 请仔细阅读以下链接: http//symfony.com/doc/current/book/security.html

Starting from here http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form it shows how to get the user from a database. 从这里开始http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form它显示了如何从数据库中获取用户。 If you use symfony's security component then you will be able to get current logged in user as follows: 如果您使用symfony的安全组件,那么您将能够获得当前登录用户,如下所示:

public function indexAction()
{
    $user = $this->get('security.context')->getToken()->getUser();
}

In a controller this can be shortcut to: 在控制器中,这可以是以下的快捷方式:

public function indexAction()
{
    $user = $this->getUser();
}

And last in template if you are using twig: 如果你使用树枝,最后在模板中:

<p>Username: {{ app.user.username }}</p>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM