在 Magento 中生成自定义 URL

2021-12-19 00:00:00 php magento clean-urls

我目前正在尝试使用 magento 生成自定义 url/路由,目前我已经在本地模块的 config.xml 中设置了默认路由.

I am currently looking at trying to generate custom urls/routing using magento, currently I have set a default route in config.xml within the local module.

<frontend>
 <routers>
         <portfolios>
             <use>standard</use>
             <args>
                 <module>Custom_Portfolios</module>
                 <frontName>portfolios</frontName>
             </args>
         </portfolios>
     </routers>
     <default>
         <router>portfolios</router>
     </default>
 </frontend>

这当前适用于/portfolios/index/action/custom-string 的 url 路径,这是 magento 默认路由.我想要实现的是让/portfolios/custom-string.html 我尝试使用 mod_rewrite 规则但没有成功,我找到了一些关于使用 .html 的自定义后缀的参考,我已将其添加到相同的 config.xml 文件.

This currently works with the url path of /portfolios/index/action/custom-string which is the magento default route. What I am trying to achieve is to have /portfolios/custom-string.html I have attempted to use a mod_rewrite rule with no success, I have found some references in relation to utilising a custom suffix of .html which I have added to the same config.xml file.

<default><portfolios><seo><portfolios_url_suffix>.html</portfolios_url_suffix></seo></portfolios></default>

我查看了与路由相关的 alan Storm 文档,发现它仅与默认路由路径相关,或者信息有点过时.

I have looked at the alan storm docs in relation to routing and found it relevent to the default routing paths only or the information is a little out-dated.

您是否知道在 magento 中控制路由的最佳方法以及可能易于遵循和相关的教程?如果是这样,请分享 :D 很多

Do you know the best method to control the routing within magento with possibly an easy to follow and relevent tutorial? if so please share :D many

推荐答案

实现此目的的方法是重写 URL.事实上,您发现的后缀配置可能被 Mage_Catalog 用于创建它的重写集.我是第一次接触这个特殊的功能,所以这个片段应该加一点盐......

The way to do this is with an URL rewrite. In fact, the suffix config you found is probably used by Mage_Catalog to create it's sets of rewrites. I'm approaching this particular feature for the first time so this snippet should be taken with a pinch of salt...

// Creating a rewrite
/* @var $rewrite Mage_Core_Model_Url_Rewrite */
$rewrite = Mage::getModel('core/url_rewrite');
$rewrite->setStoreId($store_id)
        ->setIdPath('portfolios/'.$url_key)
        ->setRequestPath('portfolios/'.$url_key.'.html')
        ->setTargetPath('portfolios/index/action/id/'.$url_key)
        ->setIsSystem(true)
        ->save();

每个可能的路径都需要一个新的重写.

A new rewrite is needed for each possible path.

编辑;我添加了一个 setIdPath 因为它可能是必要的.

Edit; I've added a setIdPath because it might be necessary.

相关文章