Prestashop 1.6 - 在自定义模块中创建前端页面
我目前在将功能添加到Prestashop本地商店定位器的自定义模块上工作。Prestashop 1.6 - 在自定义模块中创建前端页面
为了我的需要,我必须在我的模块(和第二个控制器)中创建第二个自定义页面。
我尝试了一些东西,但没有任何工作。
我在目录覆盖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文件 - > /模块/视图/模板/front/storepage.tpl
而结束,我抹去 “class_index.php” 我试试,看我的网页与链接:?
本地主机/的Prestashop/FR/index.php的控制器= storepage
我不明白为什么没有工作。
这里我们将在名为CustomStores
的模块中创建一个名为myStore
的新控制器。我们会考虑到您已经忽略了默认的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()
方法内:
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;"
);
此代码并没有进行测试,并写在一个单杆,但所有你需要的概念在这里)。如果您想了解更多信息,我建议您查看其他核心模块代码。玩的开心 !
几分钟前,我发现文档让我明白自己的错误。 而你确认这个错误。 你的CustomStore只是一个例子,说明如何在模块控制器中调用和使用自定义类。再次感谢您的帮助! –
是的。我应该补充一点,你可以使用这个URL访问你的控制器:'http://mywebsite.com/index.php?fc = module&module = customstores&controller = mystore&id_store = 1'你可以在'SEO'部分的backoffice中定义一个友好的URL。 –
我认为我必须操纵和理解pretashop核心以更多设施进行编码。 我开始同化,并感谢你的大力帮助。 –
当你说什么都没有工作,究竟发生了什么? –
如果我记得没错,你在前面的问题中覆盖了默认的StoresController.php。这个覆盖是否工作?在这里,您正在尝试创建第二个控制器,但您使用覆盖语法。我将发布一个模块控制器创建的例子作为答案。 –
嗨, 当我尝试访问此自定义控制器时出现错误404页面。 是的,这是另一个我想创建的控制器。 我的自定义StoresController完美工作。 在这里,我想创建一个自定义的页面,显示来自我的StoresController的一些信息。 –