【问题标题】:SECURITY TOKEN in first Prestashop 1.7 module第一个 Prestashop 1.7 模块中的安全令牌
【发布时间】:2022-01-26 23:01:38
【问题描述】:

我已经尝试了几天来创建一个模块。 它会很简单,将一些变量添加到不同的表中,并在您从下拉列表中选择时将它们显示在列表中。 我只是还在管理中,前台我稍后再创建,

我遵循易于遵循的教程。 先复制sql到db。

CREATE TABLE pasta (
  `id` INT NOT NULL AUTO_INCREMENT,
  `sku` VARCHAR(255) NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `description` TEXT,
  `id_pasta_category` INT NOT NULL,
  `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE = InnoDB;

然后我复制 ObjectModel 类 /override/classes/fc_pasta/Pasta.php

<?php
class Pasta extends ObjectModel {
  public $id; // fields are mandatory for create/update
  public $sku;
  public $name;
  public $created;
  public $category;
  public $id_pasta_category;
  public static $definition = [
    'table' => 'pasta',
    'primary' => 'id',
    'fields' => [
      'sku' =>  ['type' => self::TYPE_STRING, 'validate' => 'isAnything', 'required'=>true],
      'name' =>  ['type' => self::TYPE_STRING, 'validate' => 'isAnything', 'required'=>true],
      'description' =>  ['type' => self::TYPE_HTML, 'validate' => 'isAnything',],
      'created' =>  ['type' => self::TYPE_DATE, 'validate' => 'isDateFormat'],
      'id_pasta_category' => ['type'=>self::TYPE_INT, 'validate'=>'isUnsignedInt','required'=>true,],
    ],
  ];
}

然后我复制模块/modules/fc_pasta/fc_pasta.php

<?php
if (!defined('_PS_VERSION_')) {exit;}
class Fc_Pasta extends Module {
  public function __construct() {
      $this->name = 'fc_pasta'; // must match folder & file name
      $this->tab = 'administration';
      $this->version = '1.0.0';
      $this->author = 'Florian Courgey';
      $this->bootstrap = true; // use Bootstrap CSS
      parent::__construct();
      $this->displayName = $this->l('PrestaShop Module by FC');
      $this->description = $this->l('Improve your store by [...]');
      $this->ps_versions_compliancy = ['min' => '1.7', 'max' => _PS_VERSION_];
      // install Tab to register AdminController in the database
      $tab = new Tab();
      $tab->class_name = 'AdminPasta';
      $tab->module = $this->name;
      $tab->id_parent = (int)Tab::getIdFromClassName('DEFAULT');
      $tab->icon = 'settings_applications';
      $languages = Language::getLanguages();
      foreach ($languages as $lang) {
          $tab->name[$lang['id_lang']] = $this->l('FC Pasta Admin controller');
      }
      $tab->save();
  }
}

然后我创建了 AdminPastaController

<?php
require_once _PS_ROOT_DIR_.'/override/classes/fc_pasta/Pasta.php';

class AdminPastaController extends ModuleAdminController {
  public function __construct(){
      parent::__construct();
        
        // Base
        $this->bootstrap = true; // use Bootstrap CSS
        $this->table = 'pasta'; // SQL table name, will be prefixed with _DB_PREFIX_
        $this->identifier = 'id'; // SQL column to be used as primary key
        $this->className = 'Pasta'; // PHP class name
        $this->allow_export = true; // allow export in CSV, XLS..

        // List records
        $this->_defaultOrderBy = 'a.sku'; // the table alias is always `a`
        $this->_defaultOrderWay = 'ASC';
        $this->_select = 'a.name as `pastaName`, cl.name as `categoryName`';
        $this->_join = '
            LEFT JOIN `'._DB_PREFIX_.'category` cat ON (cat.id_category=a.id_pasta_category)
            LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cat.id_category=cl.id_category and cat.id_shop_default=cl.id_shop)';
        $this->fields_list = [
            'id' => ['title' => 'ID','class' => 'fixed-width-xs'],
            'sku' => ['title' => 'SKU'],
            'pastaName' => ['title' => 'Name', 'filter_key'=>'a!name'], // filter_key mandatory because "name" is ambiguous for SQL
            'categoryName' => ['title' => 'Category', 'filter_key'=>'cl!name'], // filter_key mandatory because JOIN
            'created' => ['title' => 'Created','type'=>'datetime'],
        ];

        // Read & update record
        $this->addRowAction('details');
        $this->addRowAction('edit');
        $categories = Category::getCategories($this->context->language->id, $active=true, $order=false); // [0=>[id_category=>X,name=>Y]..]
        $categories = [['id'=>1, 'display'=> 'abc'], ['id'=>2, 'display'=>'def']];
        $this->fields_form = [
            'legend' => [
            'title' => 'Pasta',
            'icon' => 'icon-list-ul'
        ],
      'input' => [
        ['type'=>'html','html_content'=>'<div class="alert alert-info">Put here any info content</div>'],
    ['name'=>'id_xxx','label'=>'XXX','type'=>'select',
      'options'=>[ 'query'=>$categories,
        'id'=>'id', // use the key id as the <option> value
        'name'=> 'display', // use the key display as the <option> title
      ]
    ],
        ['name'=>'name','type'=>'text','label'=>'Name','required'=>true],
        ['name'=>'description','type'=>'textarea','label'=>'Description',],
        ['name'=>'created','type'=>'datetime','label'=>'Created',],
        ['name'=>'id_pasta_category','label'=>'Category','type'=>'select','required'=>true,'class'=>'select2',
          'options'=>[ 'query'=>$categories,
            'id'=>'id_category', // use the key "id_category" as the <option> value
            'name'=> 'name', // use the key "name" as the <option> title
        ]],
      ],
      'submit' => [
        'title' => $this->trans('Save', [], 'Admin.Actions'),
      ]
    ];
  }
   protected function getFromClause() {
     return str_replace(_DB_PREFIX_, '', parent::getFromClause());
 }
}

几乎一切正常,但每次我更新页面时,它都会创建一个新菜单: FC Pasta Admin 控制器,我现在有 25 个,而不是 1 个。

我找不到原因。

还有一件事,我在模块中所做的一切都会得到 INVALID SECURITY TOKEN

我对创建 PS 1.7 模块非常陌生,但我真的很想尝试。

【问题讨论】:

    标签: php prestashop-1.7


    【解决方案1】:

    您正在模块构造函数中创建并保存一个新的后台选项卡, 因此每次实例化模块对象时都会创建一个新选项卡。

    您需要在模块的 install() 方法中移动选项卡创建登录,以便在第一个模块安装期间只创建一次选项卡。

    【讨论】:

    • 好的,谢谢你的回答,每次有新的出现我都快疯了。但我现在不知道把它放在哪里。
    猜你喜欢
    • 2018-05-15
    • 2023-04-09
    • 2023-01-23
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-06
    • 1970-01-01
    相关资源
    最近更新 更多