Prestashop 1.6 中自定义模块的 URL 链接 [英] URL link for custom module in Prestashop 1.6

查看:103
本文介绍了Prestashop 1.6 中自定义模块的 URL 链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个自定义模块.我想要的是有一个不错的 URL,因为现在它看起来像这样:

I am currently developing a custom module. What I want is to have a nice URL, because right now it looks like this:

domain.com/flower-deliveries?city=Hamburg&id_country=1&country=Germany

我已经添加了一个新页面来链接到自定义模块,页面名称是花递送,但我仍然有必须隐藏"的参数.

I already added a new page to link to the custom module, the page name is flower-deliveries, but still I have the parameters that I have to "hide".

相反,我想要一个这样的 URL:

Instead, of that link above I would like a URL like this:

domain.com/flower-deliveries-1-Hamburg-Germany.html

我尝试了 2 种方法,但都没有奏效..

I tried 2 methods, but none of them worked..

第一个是在我的控制器中添加一个 hookModuleRoutes,如下所示:

The first one, was to add a hookModuleRoutes in my controller, just like below:

public function hookModuleRoutes($params)
{
    return array(
        'module-vpages-dpage' => array(
            'controller' => 'dpage',
            'rule' => 'flower-deliveries{-:id_country}{-:country}{-:city}.html',
            'keywords' => array(
                'id_country' => array('regexp' => '[_a-zA-Z0-9_-]+', 'param' => 'id_country'),
                'city' => array('regexp' => '[\w]+', 'param' => 'city'),
                'country' => array('regexp' => '[\w]+', 'param' => 'country')
            ),
            'params' => array(
                'fc' => 'module',
                'module' => 'vpages',
                'controller' => 'dpage'
            )
        )
    );      
}

然后,在控制器中安装:

And then, in the controllers install:

$this->registerHook('moduleRoutes');

那没有用,所以我尝试通过添加自定义模块路由来覆盖 Dispatcher 类:

That didn't worked, so I tried to override the Dispatcher class, by adding a custom module route:

'module-vpages-dpage' => array(
    'controller' => 'dpage',
    'rule' => 'flower-deliveries{-:id_country}{-:country}{-:city}.html',
    'keywords' => array(
        'id_country' => array('regexp' => '[0-9]+', 'param' => 'id_country'),
        'city' => array('regexp' => '[\w]+', 'param' => 'city'),
        'country' => array('regexp' => '[\w]+', 'param' => 'country'),
    ),
    'params' => array(
        'fc' => 'module',
        'module' => 'vpages',
        'controller' => 'dpage'
    )
),

使用该自定义规则时,链接 http://domain.com/flower-deliveries?city=Hamburg&id_country=1&country=Germanyhttp://domain.com/flower-deliveries?module_action=list 并没有奏效,而是将我重定向到第一页.

When using that custom rule, the link http://domain.com/flower-deliveries?city=Hamburg&id_country=1&country=Germany was tranformed in http://domain.com/flower-deliveries?module_action=list and it didn't worked and was redirecting me to the first page.

有人能告诉我我做错了什么吗?我花了几个小时阅读它应该如何完成,它应该和上面的一样..

Could some one tell me what am I doing wrong? I've spent hours of reading how it should be done and it should be just like the ones above..

谢谢!

推荐答案

还原您所做的所有修改 :).

Revert all modify that you have done :).

试试这个:

例如,这是核心模块文件rootofps/modules/vpages/vpages.php

For example, this is core module file rootofps/modules/vpages/vpages.php

class VPages extends Module {
   public function __construct(){
       $this->name = 'vpages';
       $this->author = 'you';
       $this->tab = 'front_office_features';
       $this->version = '1.0.0';
       $this->controllers = array('dpage');

       parent::__construct();
   }

   // This is the function in your core module file (not in controller)
   public function install(){
       return parent::install() && $this->registerHook('moduleRoutes')
   }

   public function hookModuleRoutes($params){
      $my_link = array(
          'vpages' => array(
              'controller' => 'dpage',
              'rule' => 'flower-deliveries{-:id_country}{-:country}{-:city}.html',
              'keywords' => array(
                  'id_country' => array('regexp' => '[0-9]+', 'param' => 'id_country'),
                  'country' => array('regexp' => '[\w]+', 'param' => 'country'),
                  'city' => array('regexp' => '[\w]+', 'param' => 'city'),
               ),
               'params' => array(
                   'fc' => 'module',
                   'module' => 'vpages'
               )
           )
        );
        return $my_link;
    }
}

现在控制器rootofps/modules/vpages/controllers/front/dpage.php

class VpagesDpageModuleFrontController extends ModuleFrontController {
    public function init(){
        parent::init();
        $this->setTemplate('dapage.tpl');
    }
}

现在视图 rootofps/modules/vpages/views/templates/front/dpage.tpl

id_country = {$smarty.get.id_country}<br>
country = {$smarty.get.country}<br>
city={$smarty.get.city}<br>

这个骨架"的工作效率为 100% :),顺便说一句,请注意,如果您提供这样的网址 mydomain.com/flower-deliveries?id_country=1&country=italy&city=rome PrestaShop 不会根据您的需要将您的网址转换为清晰的网址.但是像这样的 url mydomain.com/flower-deliveries-2-italy-rome.html 将正确路由:)

This 'skeleton' works at 100% :), by the way, notice that if you give an url like this mydomain.com/flower-deliveries?id_country=1&country=italy&city=rome PrestaShop will not transform your url in a clearly url as you want. But an url like this mydomain.com/flower-deliveries-2-italy-rome.html will be routes properly :)

这篇关于Prestashop 1.6 中自定义模块的 URL 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆