Prestashop 1.6重写FrontController.php以添加自定义钩子

时间:2017-03-26 15:49:09

标签: php override prestashop prestashop-1.6 front-controller

我尝试为自定义模块添加自定义挂钩。

如果我将它直接添加到 initContent()内部的FrontController.php (/ classes / controller / FrontController.php),并在 displayHeader()< / strong>函数新定义变为:

$this->context->smarty->assign(array(
        'HOOK_HEADER'       => $hook_header,
        'HOOK_TOP'          => Hook::exec('displayTop'),    
        'HOOK_MYCUSTOM' => Module::hookExec('myCustomHook'), /* <- my hook */       
        'HOOK_LEFT_COLUMN'  => ($this->display_column_left  ? Hook::exec('displayLeftColumn') : ''),
        'HOOK_RIGHT_COLUMN' => ($this->display_column_right ? Hook::exec('displayRightColumn', array('cart' => $this->context->cart)) : ''),
        'HOOK_FOOTER'       => Hook::exec('displayFooter')
    ));

完美无缺......

但是如果我尝试通过添加包含以下内容的 /modules/myModule/override/classes/controller/FrontController.php 文件来覆盖FrontControlle.php:

class FrontController extends FrontControllerCore
{
    public function initContent()
    {
        $this->process();

        if (!isset($this->context->cart))
                $this->context->cart = new Cart();

        if ($this->context->getMobileDevice() == false) {

            if (!isset($this->context->cart))
                $this->context->cart = new Cart();

                $this->context->smarty->assign(array(

                'HOOK_HEADER' => Hook::exec('displayHeader'),
                'HOOK_TOP' => Hook::exec('displayTop'),
                'HOOK_LEFT_COLUMN' => ($this->display_column_left ? Hook::exec('displayLeftColumn') : ''),
                'HOOK_RIGHT_COLUMN' => ($this->display_column_right ? Hook::exec('displayRightColumn', array('cart' => $this->context->cart)) : ''),

                'HOOK_MYCUSTOM' => Module::hookExec('myCustomHook') /* <- my hook */                    
            ));

        } else {
            $this->context->smarty->assign(array(
                'HOOK_MOBILE_HEADER' => Hook::exec('displayMobileHeader'),
            ));
        }
    }


   public function displayHeader($display = true){ 

      if (!self::$initialized)          
        parent::init();         
        self::$smarty->assign(array(            
                'HOOK_MYCUSTOM' => Module::hookExec('myCustomHook') /* <- my hook */    
            ));

            return parent::displayHeader();     
        }  
    }

我得到以下内容:

  

注意:未定义的索引:HOOK_MYCUSTOM in   /home6/viralcom/public_html/osgaming/tools/smarty/sysplugins/smarty_internal_templatebase.php(157)   :第54行的eval()&#39代码

     

注意:尝试获取非对象的属性   /home6/viralcom/public_html/osgaming/tools/smarty/sysplugins/smarty_internal_templatebase.php(157)   :第54行的eval()&#39代码

请您帮我弄清楚上面的代码有什么问题?

解决了问题!

我已更改为文件 /modules/myModule/override/classes/controller/FrontController.php

行:

'HOOK_MYCUSTOM' => Module::hookExec('myCustomHook')

with:

'HOOK_MYCUSTOM' => Hook::exec('myCustomHook')

此后我添加了一个自动删除文件的功能:

/cache/class_index.php

进入我模块的安装功能:

public function install() {
    if (!parent::install() OR !$this->installDB() OR !$this->delecache()) 
    return false;

[...]

}

public function delecache(){
    if(!unlink($_SERVER['DOCUMENT_ROOT']."/cache/class_index.php"))
    return false;

    return true;    
}

现在工作正常!谢谢大家!

1 个答案:

答案 0 :(得分:0)

您是否删除了位于/ cache /文件夹中的 class_index.php 文件?

每次覆盖一个类时,都必须删除该文件。