繁体   English   中英

如何在symfony2中使用控制器扩展Listener?

[英]How to extend Listener with controller in symfony2?

我正在根据此讨论创建一个子域侦听器Symfony2路由-路由子域

这样就交给了这个听众,我可以做我想做的事情。

但是我无法使用我的一个控制器扩展此侦听器。 侦听器代码是这样的...

namespace Acme\FrontEndBundle\Listener;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Response;
use Acme\BraPrintBundle\Controller\BraPrintController;
use Symfony\Component\HttpFoundation\RedirectResponse;

use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;

class SubdomainListener extends BraPrintController
{
   public function onDomainParse(Event $event)
   {
       $request = $event->getRequest();
       $session = $request->getSession();
       echo $request->getHost();
       echo $this->isLoggedIn(); // defined in BraprintController
       // todo: parsing subdomain to detect country
       //do some auth stuff
       //$session->set('corporate', $request->getHost());
   }
}

但是当我尝试运行时会抛出

Fatal error: Call to a member function get() on a non-object in /home/myname/myproject/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php on line 192

因此,当我尝试调试时,确实会通过扩展类,但最终在Controller中,它无法处理get()。

在侦听器中是否可以访问控制器功能?

use Symfony\\Component\\HttpFoundation\\Request;

$request = Request::createFromGlobals();

这是一种以更简单的方式与HTTP请求和响应进行交互的替代方法。

暂无
暂无

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

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