我做了類似的檢查子域沒有改變。你可以把監聽器作爲一個服務於你的配置文件,如下所示:
services:
page_load_listener:
class: Acme\SecurityBundle\Controller\PageLoadListener
arguments:
security: "@security.context",
container: "@service_container"
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 64 }
我不能完全肯定,如果它被設置得太高也不會休息之前運行我如何優先級的作品,但發現的應用程序。這是在我的待辦事項清單上進一步研究。
下面是聽衆如何看的例子。
namespace Acme\SecurityBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class PageLoadListener extends controller
{
private $securityContext;
protected $container;
protected $query;
public function __construct(SecurityContext $context, $container, array $query = array())
{
$this->securityContext = $context;
$this->container = $container;
$this->query = $query;
}
public function onKernelRequest(GetResponseEvent $event)
{
//if you are passing through any data
$request = $event->getRequest();
//if you need to update the session data
$session = $request->getSession();
//Whatever else you need to do...
}
}
我不知道的將其設置爲只對某些頁面運行的最佳方式,但我最好的猜測是,檢查路線,只打你的分貝它當路由匹配你設置的。
希望能讓你開始!
Greg
感謝您的信息格雷格!我會玩一玩,看看我能想出什麼......我甚至可以通過檢查每個頁面加載情況而得到解決辦法(所有查詢都會檢查數據庫以查看是否存在是今天爲用戶輸入或不是)..再次感謝! – Justin
沒問題。我不會過多擔心它會在每次頁面加載時調用監聽器函數,它是一種非常小的服務,它不應該對性能產生任何影響,只是試着找出一種避免監聽器每次碰到DB的方法除非它特別需要。讓我知道你如何去,如果你有任何問題,很樂意嘗試和幫助。 – greg
我正在做同樣的事情,但如果在偵聽器查詢中找不到任何內容,我想將所有內容短路並重定向到「即將推出」模板頁面。關於如何在監聽器中構建它的任何想法?謝謝! – Acyra