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.
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 !