我目前正致力于为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文件放在这个目录中 - &gt; /module/views/templates/front/storepage.tpl
要完成,我会删除“class_index.php”并尝试通过此链接查看我的页面:
本地主机/的Prestashop / FR /的index.php?控制器= storepage
我不明白为什么没有什么工作。
答案 0 :(得分:3)
在这里,我们将在名为drawnow
的模块中创建一个名为myStore
的新Controller。我们会考虑您已经覆盖了默认的CustomStores
。
您的模块如下所示:
StoresController.php
现在你想要一个新的控制器,它不是一个覆盖。我们将通过扩展/customstores
/customstores.php
/config.xml
/override
/controllers
/front
StoresController.php
/views
/templates
/front
/stores.tpl
来创建这个新的控制器。
您的模块树现在看起来像这样:
ModuleFrontController
以下是/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();
}
}
方法中创建customstores_store
表:
install()
此代码未经过测试,只是一次性编写,但您需要的所有概念都在这里;)。如果您想了解更多信息,我建议您查看其他核心模块代码。玩得开心!