微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Prestashop 1.7 自定义模块前端控制器不起作用

如何解决Prestashop 1.7 自定义模块前端控制器不起作用

试图创建一个有奇怪行为的前端控制器模块 使用 tpl 页面中的 $link->getModuleLink('vpages','dpage',$params),URL 将正确显示mysite.com/en/40-flower-delivery-Andorra.html。尝试访问该页面时,我将看到一个 404 页面,并且 url 将变为 mysite.com/en/index.PHP?controller=dpage&id_country=40&country=Andorra&module=vpages

如果我通过添加 &fc=module 手动更改 url,我将得到一个 500 内部错误页面

该模块具有以下脚本:

<?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;
    }
}

前端控制器有以下脚本:

<?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');
    }


}

在 tpl 文件中,我使用以下脚本生成友好的 url:

{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',$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}

我不明白代码有什么问题,在 PS 1.6 中,代码在 PS 1.7 中完美运行,但在 PS 1.7 中却没有 有人能指出我的错误吗?

谢谢!

解决方法

链接应该在模块的类中创建,而不是在 tpl 中,tpl 仅用于布局。 在你的班级中使用这个 $this->context->link->getModuleLink('your_module_name','ajax',[],null,true) 然后用 smarty 将其分配给您的 tpl。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。