Prestashop 1.6 覆盖 FrontController.php 用于添加自定义挂钩

Prestashop 1.6 overriding FrontController.php for adding custom hook

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

如果我直接将它添加到 FrontController.php (/classes/controller/FrontController.php) 里面 initContent() 和内部 displayHeader() 函数新定义变为:

$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();     
        }  
    }

我得到以下信息:

Notice: Undefined index: HOOK_MYCUSTOM in /home6/viralcom/public_html/osgaming/tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 54

Notice: Trying to get property of non-object in /home6/viralcom/public_html/osgaming/tools/smarty/sysplugins/smarty_internal_templatebase.php(157) : eval()'d code on line 54

你能帮我看看上面的代码有什么问题吗?


问题已解决!


我已经改成文件/modules/myModule/override/classes/controller/FrontController.php

行:

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

与:

'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;    
}

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

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

每次覆盖 class 时,都必须删除此文件。