Prestashop 1.6 - 在自定义模块中创建前端页面

Prestashop 1.6 - Create front end page in custom module

我目前正在开发一个自定义模块,该模块向 Prestashop 本机商店定位器添加功能。

根据我的需要,我必须在我的模块(和第二个控制器)中创建第二个自定义页面。

我尝试了一些但没有任何效果。

我覆盖了我目录中的 FrontController 文件 -> /module/override/controllers/front/StorepageController.php

<?php

class StorepageController extends FrontController
{

    public function initContent()
    {
      parent::initContent();

      $this->setTemplate(_PS_MODULE_DIR_.'modulename/views/templates/front/storepage.tpl');
    }

然后我将我的 .tpl 文件放在这个目录中 -> /module/views/templates/front/storepage.tpl

最后,我擦除 "class_index.php" 并尝试使用此 link 查看我的页面:

localhost/prestashop/fr/index.php?controller=storepage

我不明白为什么没有任何效果。

这里我们将在名为 CustomStores 的模块中创建一个名为 myStore 的新控制器。我们会考虑到您已经覆盖默认 StoresController.php.

您的模块如下所示:

/customstores
    /customstores.php
    /config.xml
    /override
        /controllers
            /front
                StoresController.php
    /views
        /templates
            /front
                /stores.tpl

现在你想要一个新的控制器,它不是替代品。我们将通过扩展 ModuleFrontController.

创建这个新的控制器

您的模块树现在将如下所示:

/customstores
    /customstores.php
    /config.xml
    /override
        /controllers
            /front
                StoresController.php
    /views
        /templates
            /front
                /stores.tpl
                /mystore.tpl
    /controllers
        /front
            /mystore.php
    /classes
        /CustomStore.php

以下是mystore.php代码:

<?php

include_once(_PS_MODULE_DIR_ . 'customstores/classes/CustomStore.php');

/**
 * the name of the class should be [ModuleName][ControllerName]ModuleFrontController
 */
class CustomStoresMyStoreModuleFrontController extends ModuleFrontController
{
    public function __construct()
    {
        parent::__construct();
        $this->context = Context::getContext();
        $this->ssl = false;
    }

    /**
     * @see FrontController::initContent()
     */
    public function initContent()
    {
        parent::initContent();

        // We will consider that your controller possesses a form and an ajax call

        if (Tools::isSubmit('storeAjax'))
        {
            return $this->displayAjax();
        }

        if (Tools::isSubmit('storeForm'))
        {
            return $this->processStoreForm();
        }

        $this->getContent();
    }

    public function getContent($assign = true)
    {
        $content = array('store' => null, 'errors' => $errors);

        if (Tools::getValue('id_store') !== false)
        {
            $content['store'] = new CustomStore((int) Tools::getValue('id_store'));

            if (! Validate::isLoadedObject($content['store']))
            {
                $content['store'] = null;
                $content['errors'][] = "Can't load the store";
            }
        }
        else
        {
            $content['errors'][] = "Not a valid id_store";
        }


        if ($assign)
        {
            $this->context->smarty->assign($content);
        }
        else
        {
            return $content;
        }
    }

    public function displayAjax()
    {
        return Tools::jsonEncode($this->getContent(false));
    }

    public function processStoreForm()
    {
        // Here you get your values from the controller form
        $like = Tools::getValue('like');
        $id_store = (int) Tools::getValue('id_store');
        // And process it...
        $this->context->smarty->assign(array(
            'formResult' = CustomStore::like($id_store, $like)
        ));

        // And finally display the page
        $this->getcontent();
    }
}

我还为您添加了一个名为 CustomStore.php 的自定义 class 模块,我们在这里获取代码:

<?php

class CustomStore extends ObjectModel
{
    public $id_custom_store;

    public $name;

    public $nb_likes;

    /**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'customstores_store',
        'primary' => 'id_custom_store',
        'fields' => array(
            'name' =>     array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 128),
            'nb_likes' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
        ),
    );

    public static function like($id_store, $like)
    {
        $store = new CustomStore($id_store);

        if (! Validate::isLoadedObject($store))
        {
            return false;
        }

        if (! ($like == 1 || $like == -1))
        {
            return false;
        }

        $store->nb_likes + (int) $like;
        $store->save();
    }
}

您必须在模块 install() 方法中创建 customstores_store table:

DB::getInstance()->execute(
    "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "customstores_store` (
      `id_custom_store` varchar(16) NOT NULL,
      `name` varchar(128) NOT NULL,
      `nb_likes` int(10) unsigned NOT NULL DEFAULT '0',
      PRIMARY KEY (`id_custom_store`)
    ) ENGINE=" . _MYSQL_ENGINE_ . " DEFAULT CHARSET=utf8;"
);

此代码未经测试,是一次性编写的,但您需要的所有概念都在这里 ;)。如果您想了解更多信息,我建议您查看其他核心模块代码。玩得开心!