覆盖Prestashop模块控制器:在哪里放置php文件?

时间:2015-11-16 22:30:03

标签: php prestashop

我试图覆盖Prestashop中的模块。我已成功覆盖模块'模板,但我不能成功覆盖模块'控制器。

新控制器类文件应该放在哪里?

我尝试了以下位置,但他们没有添加新行为(更改任何内容):

  

〜/重写/模块/ blockwishlist /控制器/前/ mywishlist.php
  〜/ themes / MY_THEME / modules / blockwishlist / controllers / front / mywishlist.php

根据我的previous question,我可以通过编辑核心类(由u / Sergii P友好建议)来做到这一点,但我确信有一种标准方法可以做到这一点并不涉及编辑核心类?

供参考;这是mywishlist.php

的内容
<?php

if (!defined('_CAN_LOAD_FILES_'))
    exit;

//class BlockWishListMyWishListModuleFrontController extends BlockWishListMyWishListModuleFrontControllerCore // extends ModuleFrontController
class BlockWishListMyWishListModuleFrontControllerOverride extends BlockWishListMyWishListModuleFrontController
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Assign wishlist template
     */
    public function assign()
    {
        $errors = array();

        ....

        $this->setTemplate('mywishlist.tpl');
    }

}

编辑:我有一种可能的解决方法,因为无法覆盖ModuleFrontController类。目标是添加“导出到CSV”和“#”;按钮到My Wishlists页面,当单击该按钮时,服务器将生成一个CSV文件,其中包含该愿望清单中的所有产品。在我完成所有工作之前,您能就这是否可能提供建议......

  • 将挂钩放在模板文件中,该文件将调用自定义挂钩并链接到自定义模块{hook h='displayExportToCsvColumn' mod='myCustomModule'}
  • 创建一个注册新钩子的自定义模块,有一个呈现表列和按钮的方法,并有一个生成CSV文件的方法。
  • 大问题:你能在一个模块里面有一个模块吗?我编辑的模板文件位于模块BlockWishlist〜/ themes / MY_THEME / modules / blockwishlist / controllers / front / mywishlist.php )内,然后我的钩子将调用我的自定义模块。这可能吗?

2 个答案:

答案 0 :(得分:1)

据我所知,你现在无法覆盖ModuleFrontController(很遗憾地说)。线索位于Dispatcher::dispatch()

    case self::FC_MODULE :
        $module_name = Validate::isModuleName(Tools::getValue('module')) ? Tools::getValue('module') : '';
        $module = Module::getInstanceByName($module_name);
        $controller_class = 'PageNotFoundController';
        if (Validate::isLoadedObject($module) && $module->active) {
            $controllers = Dispatcher::getControllers(_PS_MODULE_DIR_.$module_name.'/controllers/front/');
            if (isset($controllers[strtolower($this->controller)])) {
                include_once(_PS_MODULE_DIR_.$module_name.'/controllers/front/'.$this->controller.'.php');
                $controller_class = $module_name.$this->controller.'ModuleFrontController';
            }
        }
        $params_hook_action_dispatcher = array('controller_type' => self::FC_FRONT, 'controller_class' => $controller_class, 'is_module' => 1);
    break;

它只检查/modules/?/controllers/目录。您无法覆盖ModuleFrontController类,但如果您足够聪明,则可以覆盖Dispatcher类并使其扫描ModuleFrontController的覆盖。

通常有一种方法可以通过钩子改变你想要的东西,最坏的情况 - 通过钩子注入javascript并改变内容。

答案 1 :(得分:0)

嘿,我也一直在努力解决这个问题。 我最终在prestashop论坛上发现了一个解决方案: https://www.prestashop.com/forums/topic/480523-override-front-controller-of-modules/

用户Alex为分派器创建了一个替代,它将检查任何模块控制器替代。

该文件似乎不再在论坛上可用,但是您仍然可以在这里找到它,以及有关其工作原理的一些说明: http://nemops.com/overriding-modules-controllers-in-prestashop-1-6/#.XDR7HHX0muU

如果这些代码都不再起作用了,则代码如下:

    <?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class Dispatcher extends DispatcherCore
{

    public function dispatch()
    {
        $controller_class = '';

        // Get current controller
        $this->getController();
        if (!$this->controller) {
            $this->controller = $this->useDefaultController();
        }
        // Dispatch with right front controller
        switch ($this->front_controller) {
            // Dispatch front office controller
            case self::FC_FRONT :
                $controllers = Dispatcher::getControllers(
                    array(_PS_FRONT_CONTROLLER_DIR_, _PS_OVERRIDE_DIR_.'controllers/front/')
                );
                $controllers['index'] = 'IndexController';
                if (isset($controllers['auth'])) {
                    $controllers['authentication'] = $controllers['auth'];
                }
                if (isset($controllers['compare'])) {
                    $controllers['productscomparison'] = $controllers['compare'];
                }
                if (isset($controllers['contact'])) {
                    $controllers['contactform'] = $controllers['contact'];
                }

                if (!isset($controllers[strtolower($this->controller)])) {
                    $this->controller = $this->controller_not_found;
                }
                $controller_class = $controllers[strtolower($this->controller)];
                $params_hook_action_dispatcher = array(
                    'controller_type' => self::FC_FRONT,
                    'controller_class' => $controller_class,
                    'is_module' => 0,
                );
                break;

            // Dispatch module controller for front office
            case self::FC_MODULE :
                $module_name = Validate::isModuleName(Tools::getValue('module')) ? Tools::getValue('module') : '';
                $module = Module::getInstanceByName($module_name);
                $controller_class = 'PageNotFoundController';
                if (Validate::isLoadedObject($module) && $module->active) {
                    $controllers = Dispatcher::getControllers(_PS_MODULE_DIR_.$module_name.'/controllers/front/');

                    if (isset($controllers[strtolower($this->controller)])) {
                        include_once(_PS_MODULE_DIR_.$module_name.'/controllers/front/'.$this->controller.'.php');

                        if (file_exists(
                            _PS_OVERRIDE_DIR_.'modules/'.$module_name.'/controllers/front/'.$this->controller.'.php'
                        )) {
                            include_once(_PS_OVERRIDE_DIR_.'modules/'.$module_name.'/controllers/front/'.$this->controller.'.php');
                            $controller_class = $module_name.$this->controller.'ModuleFrontControllerOverride';
                        } else {

                            $controller_class = $module_name.$this->controller.'ModuleFrontController';
                        }
                    }
                }
                $params_hook_action_dispatcher = array(
                    'controller_type' => self::FC_FRONT,
                    'controller_class' => $controller_class,
                    'is_module' => 1,
                );
                break;

            // Dispatch back office controller + module back office controller
            case self::FC_ADMIN :
                if ($this->use_default_controller && !Tools::getValue('token') && Validate::isLoadedObject(
                        Context::getContext()->employee
                    ) && Context::getContext()->employee->isLoggedBack()) {
                    Tools::redirectAdmin(
                        'index.php?controller='.$this->controller.'&token='.Tools::getAdminTokenLite($this->controller)
                    );
                }

                $tab = Tab::getInstanceFromClassName($this->controller, Configuration::get('PS_LANG_DEFAULT'));
                $retrocompatibility_admin_tab = null;

                if ($tab->module) {
                    if (file_exists(_PS_MODULE_DIR_.$tab->module.'/'.$tab->class_name.'.php')) {
                        $retrocompatibility_admin_tab = _PS_MODULE_DIR_.$tab->module.'/'.$tab->class_name.'.php';
                    } else {
                        $controllers = Dispatcher::getControllers(_PS_MODULE_DIR_.$tab->module.'/controllers/admin/');
                        if (!isset($controllers[strtolower($this->controller)])) {
                            $this->controller = $this->controller_not_found;
                            $controller_class = 'AdminNotFoundController';
                        } else {
                            // Controllers in modules can be named AdminXXX.php or AdminXXXController.php
                            include_once(_PS_MODULE_DIR_.$tab->module.'/controllers/admin/'.$controllers[strtolower(
                                    $this->controller
                                )].'.php');
                            $controller_class = $controllers[strtolower($this->controller)].(strpos(
                                    $controllers[strtolower($this->controller)],
                                    'Controller'
                                ) ? '' : 'Controller');
                        }
                    }
                    $params_hook_action_dispatcher = array(
                        'controller_type' => self::FC_ADMIN,
                        'controller_class' => $controller_class,
                        'is_module' => 1,
                    );
                } else {
                    $controllers = Dispatcher::getControllers(
                        array(
                            _PS_ADMIN_DIR_.'/tabs/',
                            _PS_ADMIN_CONTROLLER_DIR_,
                            _PS_OVERRIDE_DIR_.'controllers/admin/',
                        )
                    );
                    if (!isset($controllers[strtolower($this->controller)])) {
                        // If this is a parent tab, load the first child
                        if (Validate::isLoadedObject($tab) && $tab->id_parent == 0 && ($tabs = Tab::getTabs(
                                Context::getContext()->language->id,
                                $tab->id
                            )) && isset($tabs[0])) {
                            Tools::redirectAdmin(Context::getContext()->link->getAdminLink($tabs[0]['class_name']));
                        }
                        $this->controller = $this->controller_not_found;
                    }

                    $controller_class = $controllers[strtolower($this->controller)];
                    $params_hook_action_dispatcher = array(
                        'controller_type' => self::FC_ADMIN,
                        'controller_class' => $controller_class,
                        'is_module' => 0,
                    );

                    if (file_exists(_PS_ADMIN_DIR_.'/tabs/'.$controller_class.'.php')) {
                        $retrocompatibility_admin_tab = _PS_ADMIN_DIR_.'/tabs/'.$controller_class.'.php';
                    }
                }

                // @retrocompatibility with admin/tabs/ old system
                if ($retrocompatibility_admin_tab) {
                    include_once($retrocompatibility_admin_tab);
                    include_once(_PS_ADMIN_DIR_.'/functions.php');
                    runAdminTab($this->controller, !empty($_REQUEST['ajaxMode']));

                    return;
                }
                break;

            default :
                throw new PrestaShopException('Bad front controller chosen');
        }

        // Instantiate controller
        try {
            // Loading controller
            $controller = Controller::getController($controller_class);

            // Execute hook dispatcher
            if (isset($params_hook_action_dispatcher)) {
                Hook::exec('actionDispatcher', $params_hook_action_dispatcher);
            }

            // Running controller
            $controller->run();
        } catch (PrestaShopException $e) {
            $e->displayMessage();
        }
    }

}
  • 将此文件添加到override / classes目录
  • 删除cache / class_index.php文件

此后,可以通过将替代文件放在:

中来替代模块控制器。

override/modules/{moduleName}/controllers/{front/admin}/{filename}.php

在此文件中,您必须定义一个名称与要覆盖的类完全相同的类,并在类名的末尾附加“ Override”。

我希望这对您有帮助