PrestaShop,在ModuleAdminController :: getController中找不到类

时间:2017-03-22 10:49:49

标签: php inheritance prestashop controllers

我试图开发一个带有控制器的PrestaShop模块,例如: /modules/mymodule/controllers/admin/myControlController.php

class MyControlController extends ModuleAdminController {
public function __construct() {           
      $this->module = 'mymodule'; 
      $this->bootstrap = true;
      $this->context = Context::getContext();     
      $token = Tools::getAdminTokenLite('AdminModules');
      $currentIndex='index.php?controller=AdminModules&token='.$token.'&configure=mymodule&tab_module=administration&module_name=mymodule';
      Tools::redirectAdmin($currentIndex);
      parent::__construct();                
}
public function showForm() {
  die("hello");
}}
如果我将其称为url,

Controller工作(调用构造方法) http://myshop.com/adminxxx/index.php?controller=MyControl&token=9faf638aa961468c8563ffb030b3c4a8

但我无法从主类模块访问控制器的方法: ModuleAdminController :: getController(' MyControl') - > showForm(); 我收到了#34; Class not found"以往

这是从外部访问控件的正确方法吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

如果要显示与表单有关的任何内容,则应使用renderForm()。 你应该尝试parent :: showForm();或$ this-> showForm();。

以下是可以工作的控制器示例:

require_once _PS_MODULE_DIR_.'modulename/models/ModuleNameLog.php';
require_once _PS_MODULE_DIR_.'modulename/modulename.php';

 class AdminModuleNameLogController extends ModuleAdminController
 {
  protected $_defaultOrderBy = 'id_modulenamelog';
  protected $_defaultOrderWay = 'DESC';

  public function __construct()
  {
    $this->table = 'modulenamelog';
    $this->className = 'ModuleNameLog';

    $this->context = Context::getContext();
    $this->lang = false;
    $this->bootstrap = true;
    $this->actions_available = array();
    $this->actions = array();
    $this->show_toolbar = false;
    $this->toolbar_btn['new'] = array();
    $this->tabAccess['add'] = '0';
    $this->allow_export = true;
    $this->requiredDatabase = true;
    $this->page_header_toolbar_title = $this->l('Example Module Name logs');

    $this->_select = 'SUM(a.quantity) as total_quantity';
    $this->_group = ' GROUP BY a.id_product, a.id_product_attribute ';

    $this->fields_list = array(
        'id_product' => array(
            'title' => $this->l('Product'),
            'align' => 'center',
            'callback' => 'getProductName',
        ),
        'id_product_attribute' => array(
            'title' => $this->l('Combination'),
            'align' => 'center',
            'callback' => 'getAttributeName',
        ),
        'total_quantity' => array(
            'title' => $this->l('Total Quantity'),
            'align' => 'center',
        ),
    );

    $this->mod = new ModuleName();
    $this->mod->cleanLogs();

    $this->context = Context::getContext();

    parent::__construct();
}

public function getProductName($id)
{
    if (!empty($id)) {
        $product = new Product($id, true, $this->context->cookie->id_lang);

        return $product->name;
    }
}
public function getAttributeName($id)
{
    if (!empty($id)) {
        $combination = new Combination($id);
        $names = $combination->getAttributesName($this->context->cookie->id_lang);
        $str = array();

        if (!empty($names)) {
            foreach ($names as $value) {
                $str[] = $value['name'];
            }
        }

        return implode(' - ', $str);
    } else {
        return '-';
    }
}

public function postProcess()
{
    if (Tools::isSubmit('purge_id')) {

        // Do something here
        $id = (int) Tools::getValue('purge_id');


        Tools::redirectAdmin(self::$currentIndex.'&token='.Tools::getAdminTokenLite('AdminModuleNameLog').'&conf=4');

    }

    parent::postProcess();
}

public function renderList()
{
    $carts = Db::getInstance()->executeS('SELECT ct.*, cs.`firstname`, cs.`lastname` FROM '._DB_PREFIX_.'cart ct LEFT JOIN '._DB_PREFIX_.'customer cs ON ct.id_customer = cs.id_customer WHERE 1 ORDER BY id_cart DESC LIMIT 0,2000');

    $tpl = $this->context->smarty->createTemplate(_PS_MODULE_DIR_.'modulename/views/templates/admin/preform.tpl');

    $tpl->assign(array(
        'carts' => $carts,
    ));

    $html = $tpl->fetch();

    return $html.parent::renderList();
}

public function renderForm()
{
    if (!$this->loadObject(true)) {
        return;
    }

    $obj = $this->loadObject(true);

    if (isset($obj->id)) {
        $this->display = 'edit';
    } else {
        $this->display = 'add';
    }

    $array_submit = array(
        array(
            'type' => 'select',
            'label' => $this->l('Cart :'),
            'name' => 'id_cart',
            'options' => array(
                'query' => Db::getInstance()->executeS('SELECT * FROM '._DB_PREFIX_.'cart WHERE id_cart > 0 ORDER BY id_cart DESC LIMIT 0,500'),
                'id' => 'id_cart',
                'name' => 'id_cart',
            ),
        ),
        array(
            'type' => 'text',
            'label' => $this->l('Quantity translation here'),
            'hint' => $this->l('Description and translation here'),
            'name' => 'quantity',
        ),
    );

    $this->fields_form[0]['form'] = array(
        'tinymce' => false,
        'legend' => array(
            'title' => $this->l('Form title'),
        ),
        'input' => $array_submit,
        'submit' => array(
            'title' => $this->l('Save'),
            'class' => 'btn btn-default',
        ),
    );

    $this->multiple_fieldsets = true;

    return parent::renderForm();
}
}