Cakephp 2.3 beforeFilter 和 implementedEvents 无法共存

Cakephp 2.3 beforeFilter and implementedEvents aren't able to co-exist

使用 cakephp 2.3.5.

我在几个控制器中使用了 beforeFilter 来允许某些操作而无需登录。我已经成功地使用它很长一段时间了。但是,我注意到如果控制器也有 implementedEvents() 方法,我无法触发 beforeFilter。

public function beforeFilter() {
        parent::beforeFilter();
        $this->Auth->allow('matchWwiProducts');
}     

public function implementedEvents() {
       return array(
                    'Controller.Product.delete' => 'deleteSku',
                    'Controller.Product.price'  => 'notifySubscribers',
                    'Controller.Product.stock'  => 'notifySubscribers'
       );
}

对于上面显示的代码,如果我调用 www.example.com/products/matchWwiProducts.

方法,我将被强制登录

当我注释掉 implementedEvents() 时,一切都按预期工作。我四处搜索,找不到任何对 implementedEvents() 的引用,这会导致 beforeFilter 出现问题。

动作matchWwiProducts()如下。当我登录时,它工作得很好。但是,我不想强​​制登录才能执行此操作。

public function matchWwiProducts() {
     // this is an audit function that matches Products sourced by wwi
     //
     $this->autoRender = false; // no view to be rendered
     // retrieve products sourced from wwi from Table::Product
     $this->Product->contain();
     $wwiProducts = $this->Product->getWwiSkus();                        
     $wwiProductCount = count($wwiProducts);
     // retrieve products sourced from wwi from Table:Wwiproduct
     $this->loadModel('WwiProduct');
     $this->Wwiproduct->contain();
     $wwiSource = $this->Wwiproduct->getSkuList();
     $wwiSourceCount = count($wwiSource);                        
     // identify SKUs in $wwiProducts that are not in $wwiSource
     $invalidSkus = array_diff($wwiProducts, $wwiSource);                        
     // identify SKUs in $wwiSource that are not in $wwiProducts
     $missingSkus = array_diff($wwiSource, $wwiProducts);                        
     $missingSkuDetails = array();
     foreach ($missingSkus as $missingSku) {
             $skuStockStatus = $this->Wwiproduct->getStockStatus($missingSku);
             $missingSkuDetails[$missingSku] = $skuStockStatus;
             }                        
     $email = new CakeEmail();
     $email->config('sourcewwi');
     $email->template('sourcewwiaudit', 'sourcewwi');
     if (count($invalidSkus) > 0 || count($missingSkus) > 0) {
            $email->subject('WWI Source Audit: Invalid or Missing SKUs');
            $email->viewVars(array('invalidSkus' => $invalidSkus,                                                       
                                   'missingSkuDetails' => $missingSkuDetails,
                                   'wwiProductCount' => $wwiProductCount,
                                   'wwiSourceCount' => $wwiSourceCount));
     } else {
             $email->subject('WWI Source Audit: No Exceptions');
             $email->viewVars(array('wwiProductCount' => $wwiProductCount,
                                    'wwiSourceCount' => $wwiSourceCount));
     }
     $email->send();
}

它不会触发,因为您重载了 implementendEvents() 方法 而没有确保您将 the existing events 保持在那里。

public function implementedEvents() {
       return array_merge(parent::implementedEvents(), array(
                    'Controller.Product.delete' => 'deleteSku',
                    'Controller.Product.price'  => 'notifySubscribers',
                    'Controller.Product.stock'  => 'notifySubscribers'
       ));
}

Overloading in php.

检查大部分基础classes,Controller,Table,Behavior,Component,它们都会触发或监听事件。所以在那里扩展某些方法时要小心。最简单的方法可能是在所有 class 中搜索 "EventListenerInterface"。实现此接口的class很可能实现事件回调。