Php 重定向发送变量symfony

Php 重定向发送变量symfony,php,symfony,twig,Php,Symfony,Twig,我是Symfony的新手(正在处理我的第一个项目!),我希望将用户发送回登录页面,并显示一个错误:“您没有登录”,以防他们没有登录 是否有任何方法可以运行某些内容,从而将用户的URL从/secured更改为/login,并发送数据,如下所示: return$this->render('radloginBundle:Default:login.html.twig',array('errors'=>“您未登录”) 因此,在login.html.twig中,我有: {% if errors is de

我是Symfony的新手(正在处理我的第一个项目!),我希望将用户发送回登录页面,并显示一个错误:“您没有登录”,以防他们没有登录

是否有任何方法可以运行某些内容,从而将用户的URL从/secured更改为/login,并发送数据,如下所示:

return$this->render('radloginBundle:Default:login.html.twig',array('errors'=>“您未登录”)

因此,在login.html.twig中,我有:

{% if errors is defined %}
<div class="red">{{errors}}</div>
{% endif %}
但用户的url变成:

http://.../app_dev.php/login?errors=You+aren't+logged+in
而不是:

http://.../app_dev.php/login

您可以使用会话服务的flash包通过单个请求传递值(实际上是重定向)

这一章是关于

如果文档不够,下面是一个代码示例:

public function updateAction()
{
    // if the method returns null, the user isn't logged in
    if ($this->getUser() === null) {
        $this->get('session')->getFlashBag()->add(
            'errors', // this is a key
            'You are not logged in' // this is a value to add to key
        );

        // this will redirect to the login page if the user isn't logged in
        throw new \Symfony\Component\Security\Core\Exception\AccessDeniedException();
    }
}

然后,您可以使用文档中介绍的方法显示错误。

设置闪存消息并抛出AccessDeniedException
public function updateAction()
{
    // if the method returns null, the user isn't logged in
    if ($this->getUser() === null) {
        $this->get('session')->getFlashBag()->add(
            'errors', // this is a key
            'You are not logged in' // this is a value to add to key
        );

        // this will redirect to the login page if the user isn't logged in
        throw new \Symfony\Component\Security\Core\Exception\AccessDeniedException();
    }
}