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 !

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

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?