CakeFp 2.3 beforeFilter和implementsEvents不能共存

时间:2015-07-12 16:02:37

标签: cakephp cakephp-2.0

使用cakephp 2.3.5。

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

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,我将被迫登录。

当我注释掉implementsEvents()时,一切都按预期工作。我已经四处搜索,无法找到对implementsEvents()的任何引用,这些引用会在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();
}

1 个答案:

答案 0 :(得分:2)

它没有触发,因为你在没有的情况下重载了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.

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