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

时间:2012-10-30 05:19:31

标签: php symfony routing

我正在根据此讨论创建一个子域监听器Symfony2 Routing - route subdomains

所以这是听众,我可以做我想做的事。

但我无法使用我的一个控制器扩展此监听器。听众代码就像这样......

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()。

是否有解决方案来访问Listener中的控制器功能?

1 个答案:

答案 0 :(得分:0)

use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();

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