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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?