0
难道你们中的任何一位symfony2大师能够告诉我如何从symfony2服务写入flash消息吗?如何编写来自symfony2服务的Flash消息?
我原本以为我需要的东西,当我注入下面的容器,但显然不是,我得到错误
Fatal error: Call to undefined method appDevDebugProjectContainer::getRequest() in /var/www/cloudsign_beta/src/BizTV/CommonBundle/Helper/globalHelper.php on line 135
因此很明显,我无法访问的请求......如果我要传递以及从控制器我很快就会失去一个服务点,它是无法自己做任何事情=)
<?php
namespace BizTV\CommonBundle\Helper;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Doctrine\ORM\EntityManager as EntityManager;
use BizTV\CommonBundle\Entity\Log;
class globalHelper {
private $container;
private $em;
public function __construct(Container $container, EntityManager $em) {
$this->container = $container;
$this->em = $em;
}
public function log($type,$message) {
// currently $type can be 'success', 'fail' or 'error'.
$currentUser = $this->container->get('security.context')->getToken()->getUser();
$currentCompany = $this->container->get('security.context')->getToken()->getUser()->getCompany();
//if the $type is one that we want to write to the log then create log entity (we don't log failed attempts at operations, but we do log errors.
if ($type == 'success') {
$em = $this->em;
$now = new \DateTime("now");
$entity = new Log();
$entity->setCompany($currentCompany);
$entity->setExecutor($currentUser);
$entity->setTime($now);
$entity->setEventType($type);
$entity->setEventMessage($message);
$em->persist($entity);
$em->flush();
}
//flash out the $message message text
$container = $this->container;
$session = $container->getRequest()->getSession()->setFlash($type, $message);
}
}
非常感谢。我相信请求会一直存在,因为这只会在symfony验证表单后才会调用。 –
会不会$ container-> get('session')变短? –
是的,它也应该起作用! – iamdto