簡體   English   中英

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”,我嘗試通過此鏈接查看我的頁面:

本地主機/的Prestashop / FR /的index.php?控制器= storepage

我不明白為什么沒有什么工作。

在這里,我們將在一個名為CustomStores的模塊中創建一個名為myStore的新Controller。 我們將考慮您已經覆蓋了默認的StoresController.php

您的模塊如下所示:

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

現在你想要一個新的控制器,它不是一個覆蓋。 我們將通過擴展ModuleFrontController來創建這個新的Controller。

您的模塊樹現在看起來像這樣:

/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的自定義類,這里我們去找代碼:

<?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表:

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;"
);

這段代碼沒有經過測試,只是一次性編寫,但您需要的所有概念都在這里;)。 如果您想了解更多信息,我建議您查看其他核心模塊代碼。 玩得開心 !

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM