Search code examples
phpoverridingprestashopprestashop-1.6front-controller

Prestashop 1.6 overriding FrontController.php for adding custom hook


I tried to add a custom hook for my custom module.

If I add it directly to FrontController.php (/classes/controller/FrontController.php) inside the initContent() and inside displayHeader() functions the new definition becomes:

$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')
    ));

It works perfectly...

But if i attempt to override the FrontControlle.php by adding the /modules/myModule/override/classes/controller/FrontController.php file that contains:

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

I get the following:

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

Could you please help me figure out what is wrong with the above code?


Problem Solved!


I have changed into the file /modules/myModule/override/classes/controller/FrontController.php

the row:

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

with :

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

AFTER THIS i have added a function to automatically delete the file:

/cache/class_index.php

into the install function of my module:

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

Now work fine! Thanks to all!


Solution

  • Have you deleted the class_index.php file located in the /cache/ folder ?

    Every time you override a class, you have to delete this file.