這裏我們將在名爲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;"
);
此代碼並沒有進行測試,並寫在一個單杆,但所有你需要的概念在這裏)。如果您想了解更多信息,我建議您查看其他核心模塊代碼。玩的開心 !
當你說什麼都沒有工作,究竟發生了什麼? –
如果我記得沒錯,你在前面的問題中覆蓋了默認的StoresController.php。這個覆蓋是否工作?在這裏,您正在嘗試創建第二個控制器,但您使用覆蓋語法。我將發佈一個模塊控制器創建的例子作爲答案。 –
嗨, 當我嘗試訪問此自定義控制器時出現錯誤404頁面。 是的,這是另一個我想創建的控制器。 我的自定義StoresController完美工作。 在這裏,我想創建一個自定義的頁面,顯示來自我的StoresController的一些信息。 –