Tomáš 的链接帮助我解决了这个问题。即使它可能不是我实现它的最佳解决方案:
我创建了一个PHP 类:
namespace Acme\AdminBundle\EventListener;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class SoftDeleteListener {
public function onKernelController(FilterControllerEvent $event) {
// Get the full name of the current controller
$controllername = $event->getRequest()->attributes->get('_controller');
$matches = array();
// Explode the name of the current controller
preg_match('/(.*)\\\(.*)Bundle\\\Controller\\\(.*)Controller::(.*)Action/', $controllername, $matches);
// preg_match should store the name of the bundle at the second index of the array
if (isset($matches[2]) && $matches[2] == "Admin") {
$controller = $event->getController();
$doctrine = $controller[0]->get('doctrine');
$em=$doctrine->getManager();
$em->getFilters()->disable('softdeleteable');
}
}
}
并将这些行添加到services.yml以使其监听 kernel.controller 事件:
kernel.listener.admin_softdelete_listener:
class: Goldfish\AdminBundle\EventListener\SoftDeleteListener
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
这样我就达到了我的目标 - 整个 AdminBundle 的 softdeletable 过滤器被禁用,我不需要在每个控制器中手动关闭它。