【发布时间】:2016-02-11 12:37:43
【问题描述】:
我正在处理 Symfony2 上的 tutorial(我是 PHP 新手),我在显示来自 Session 对象的 Flash 消息时遇到了困难。 在我的 Controller 代码下方
<?php
namespace tuto\WelcomeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class HomepageController extends Controller
{
public function indexAction()
{
$this->get('session')->setFlash('notice', 'Bienvenue à toi '.$this->get('session')->get('user_name').' !');
return $this->render('tutoWelcomeBundle:Homepage:index.html.twig');
}
public function whoAmIAction($name)
{
# get the session
$session = $this->get('session');
# store the user'name in the session
$session->set('user_name', $name);
return $this->redirect($this->container->get('router')->generate('tutoWelcomeBundle_homepage'));
}
}
setFlash('notice', 'Bienvenue à toi '.$this->get('session')->get('user_name').' !') 正在返回一个“未定义的方法错误”。我确实尝试在 AppKernel.php 中将其声明为捆绑包的一部分,但找不到命名空间 Symfony\Component\HttpFoundation\Session。
下面的布局渲染它
<div id="container">
<header>
<a href="{{ path('homepage') }}" title="Retour à l'accueil">Tutoriel Symfony2</a>
Bonjour et bienvenue dans ce tutoriel pour Symfony2</p>
<p>
{% for key, flash in app.session.getFlashes()%} {%if flash%} {{flash}} {%endif%} {%endfor%}
</p>
</header>
<div id="content">
{% block content %}{% endblock %}
</div>
平台信息
WAMP 2.5
Symfony 2.7
【问题讨论】: