Reputation: 656
Trying to create a front controller module that has a strange behaviour
Using the $link->getModuleLink('vpages', 'dpage', $params)
in the tpl page, the URL will be correctly presented as mysite.com/en/40-flower-delivery-Andorra.html
. When trying to access the page, I will be provided with a 404 page and the url will become mysite.com/en/index.php?controller=dpage&id_country=40&country=Andorra&module=vpages
if I manually alter the url by adding the &fc=module
, I will get a 500 Internal error page
The module has the following script:
<?php
class vPages extends Module
{
private $html = '';
private $postErrors = array();
public function __construct()
{
$this->name = 'vpages';
$this->tab = 'others';
$this->version = '1.0';
$this->author = 'Rosu Andrei Mihai';
$this->is_eu_compatible = 1;
$this->need_instance = 1;
$this->bootstrap = true;
$this->controllers = array('dpage');
$this->displayName = $this->l('vPages');
$this->description = $this->l('Virtual dynamic pages');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->confirmUninstall = $this->l('Are you sure you want to delete all saved details?');
$this->secure_key = Tools::encrypt($this->name);
parent::__construct();
}
public function install()
{
return parent::install() && $this->registerHook('moduleRoutes');
}
public function hookModuleRoutes($params){
$my_link = array(
'module-vpages-dpage' => array(
'controller' => 'dpage',
'rule' => '{id_country:-}flower-delivery{-:country}{-:city}.html',
'keywords' => array(
'id_country' => array('regexp' => '[0-9]+', 'param' => 'id_country'),
'setCountry' => array('regexp' => '[0-9]+', 'param' => 'setCountry'),
'country' => array('regexp' => '[_a-zA-Z0-9\pL\pS]+', 'param' => 'country'),
'city' => array('regexp' => '[_a-zA-Z0-9\w\pL\pS-]*', 'param' => 'city'),
'module_action' => array('regexp' => '[\w]+', 'param' => 'module_action')
),
'params' => array(
'fc' => 'module',
'module' => 'vpages'
)
)
);
return $my_link;
}
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
return true;
}
}
The front controller has the following script:
<?php
class vpagesdpageModuleFrontController extends ModuleFrontController
{
public $errors = array();
public $display_column_left = false;
public $display_column_right = false;
public $ssl = true;
public $php_self = 'dpage';
public function __construct()
{
parent::__construct();
$this->page_name = 'dpage';
$this->context = Context::getContext();
}
public function initContent()
{
parent::initContent();
$this->context->cookie->__set('productbycountry_id',Tools::getValue('id_country'));
$sql = "SELECT DISTINCT ps_country_lang.id_country, ps_country_lang.name as country, city FROM ps_cms_target_world
LEFT JOIN ps_country_lang ON ps_cms_target_world.id_country = ps_country_lang.id_country
WHERE ps_country_lang.id_country=" . Tools::getValue('id_country') . "
ORDER BY country ASC";
//die($sql);
$cPages = Db::getInstance()->ExecuteS($sql);
$get_url = Db::getInstance()->ExecuteS('SELECT domain,physical_uri FROM '._DB_PREFIX_.'shop_url ');
$protocol = (isset($_SERVER['HTTPS']) ? "https" : "http") ;
$site_url = "$protocol://".$get_url[0]['domain'].$get_url[0]['physical_uri']."modules";
$controller_url = $this->context->link->getModuleLink('vpages', 'vPagesController', array(), true);
$this->context->smarty->assign(array(
'link' => $this->context->link,
'controller_url' => $controller_url,
'cPages' => $cPages,
'SITEURL' => $site_url
));
$country = urldecode(Tools::getValue('country'));
$city = urldecode(Tools::getValue('city'));
if(isset($city) && !empty($city)) $title = " - " . $city;
$this->context->smarty->assign('meta_title', 'Flower delivery to ' . $country . $title);
$this->setTemplate('module:vpages/views/templates/front/dPage.tpl');
}
}
In the tpl file, I generate the friendly url using the following script:
{foreach $vPages as $page}
<div class="country_content col-xs-4 col-sm-3 col-md-2 center-block text-center">
<p>
{assign var=params value=[
'module_action' => 'list',
'id_country'=> $page.id_country,
'setCountry'=> $page.id_country,
'country'=> urlencode($page.country),
'city' => null
]}
{assign var="meta_title" value="Flowers to {$page.country}"}
<a style="color:normal" href="{$link->getModuleLink('vpages', 'dpage', $params)|escape:'html':'UTF-8'}" title="{$meta_title|escape:'html':'UTF-8'}">
<img width="98" height="70" src="{$SITEURL}/vpages/flags/{$page.country}.png" style="padding-right: 3px; margin=bottom: 5px;" />
<br />
<p>{$page.country|escape:'html':'UTF-8'}</p>
</a>
</p>
</div>
{/foreach}
I don't get what is wrong in the code, in PS 1.6 the code works perfect in PS 1.7 it doesn't Could someone point me to the mistake?
Thanks!
Upvotes: 2
Views: 1995
Reputation: 1078
The link should be created in your module's class not in the tpl, a tpl is just for layout.
Use this in your class $this->context->link->getModuleLink('your_module_name', 'ajax', [], null, null, null, true)
Then assign it with smarty to your tpl.
Upvotes: 2