简体   繁体   English

Prestashop 1.7从模块中覆盖核心控制器

[英]Prestashop 1.7 override core controller from module

I am facing a strange situation. 我面临一个奇怪的情况。 I am trying to override cart controller and I am able to succeed if I place the CartController.php file in /override/controllers/front and fails if I place the same file in my custom module and installing the module. 我试图覆盖购物车控制器,如果我将CartController.php文件放在/ override / controllers / front中 ,我就能成功,如果我将相同的文件放在我的自定义模块中并安装模块,则会失败。 The path of file in module is myModule/override/controllers/front . 模块中的文件路径是myModule / override / controllers / front The file contents are 文件内容是

class CartController extends CartControllerCore
{
  public function init()
  {
    die('Override');
  }
}

I have also registered a hook and it displays fine. 我也注册了一个钩子,它显示正常。

  public function install()
  {
    if (Shop::isFeatureActive())
      Shop::setContext(Shop::CONTEXT_ALL);

    return parent::install() && $this->registerHook('Test');
  }

  public function uninstall()
  {
    if (!parent::uninstall() ||
      !Configuration::deleteByName('MYMODULE_NAME'))
      return false;
    return true;
  }

  public function hookTest($params){

    return $this->display(__FILE__, '/views/templates/hook/testpage.tpl');
  }

What am I missing here? 我在这里错过了什么?

I believe you are doing everthing ok. 我相信你做得很好。 The override in your module is copied to the override folder/file when you install the module, unless there is already another override for that class function. 安装模块时,模块中的覆盖将复制到覆盖文件夹/文件,除非已经有该类功能的另一个覆盖。 See this answer https://stackoverflow.com/a/24114184 请参阅此答案https://stackoverflow.com/a/24114184

Hi curious_coder all above are mentioned by you are correct,but you have missed how to override a file in corresponding folder. 你好上面提到的curious_coder都是正确的,但你错过了如何覆盖相应文件夹中的文件。

Write below code in your cart custom module for override file. 在购物车自定义模块中写下以下代码以覆盖文件。

protected static $overrides = array(
    'controllers/front/CartController.php'
);//Override file name

public function install(){
    if (Shop::isFeatureActive())
    Shop::setContext(Shop::CONTEXT_ALL);
    foreach(self::$overrides as $file){
        $explode = explode("/", $file);
        $file_name = $explode[count($explode)-1];
        unset($explode[count($explode)-1]);
        $folder = implode("/", $explode);
        @mkdir (_PS_OVERRIDE_DIR_.$folder, 0777, true);
        @copy ( _PS_MODULE_DIR_.$this->name.'/override/'.$folder."/".$file_name 
    , _PS_OVERRIDE_DIR_.$folder."/".$file_name );
        $old = @umask(0);
        @chmod (_PS_OVERRIDE_DIR_.$folder."/".$file_name, 0777);
        @umask($old);
    }
    return parent::install() && $this->registerHook('Test');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM