Prestashop 1.6-在自定义模块中创建前端页面 [英] Prestashop 1.6 - Create front end page in custom module

查看:76
本文介绍了Prestashop 1.6-在自定义模块中创建前端页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在一个自定义模块上工作,该模块向Prestashop本机商店定位器添加功能.

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.

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

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');
    }

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

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

最后,我擦除了"class_index.php",并尝试通过此链接查看我的页面:

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

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

我不明白为什么什么都不起作用.

I don't understand why nothing is working.

推荐答案

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

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.

您的模块如下所示:

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

现在您想要一个新的控制器,它不是替代控件.我们将通过扩展ModuleFrontController来创建新的Controller.

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

以下是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的自定义类模块,下面是代码:

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

您将必须在模块install()方法内创建customstores_store表:

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 !

这篇关于Prestashop 1.6-在自定义模块中创建前端页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆