duanlinzhen7235 2016-05-30 14:57
浏览 36
已采纳

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

I currently works on a custom module who add features to the Prestashop native store locator.

For my needs, i must create a second custom page in my module (and a second controller).

I tried something but nothing works.

I override FrontController file in my directory -> /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');
    }

And i put my .tpl file in this directory -> /module/views/templates/front/storepage.tpl

And to finish, i erase "class_index.php" and i try to see my page with this link :

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

I don't understand why nothing is working.

  • 写回答

1条回答 默认 最新

  • dongzhidian3538 2016-05-31 09:00
    关注

    Here we will create a new Controller called myStore in a module called CustomStores. We will take into account that you already overrided the default StoresController.php.

    Your module looks like this:

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

    Now you want to had a new controller, it's not an override. we will create this new Controller by extending the ModuleFrontController.

    Your module tree will now look like this:

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

    Following is mystore.php code:

    <?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();
        }
    }
    

    I've also added a custom class for you module called CustomStore.php, here we go for the code:

    <?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();
        }
    }
    

    You will have to create the customstores_store table inside your module install() method:

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

    This code was not tested and was written in a single shot, but all the concept you will need are here ;). I advise you to look at other core modules code if you want to learn more. Have fun !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测