[英]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.