10

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

<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 文件。

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

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

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

4

3 回答 3

15

下面的代码未经测试,但应该可以工作

如果您不想为每个 protfolio 项目定义自定义重写,只需按照以下步骤操作:

  1. 编写从 Mage_Core_Controller_Varien_Router_Standard 扩展的自定义路由器类并实现match方法:

    public function match(Zend_Controller_Request_Http $request)
    {
        $path = explode('/', trim($request->getPathInfo(), '/'));
        // If path doesn't match your module requirements
        if (count($path) > 2 && $path[0] != 'portfolios') {
            return false; 
        }
        // Define initial values for controller initialization
        $module = $path[0];
        $realModule = 'Custom_Portfolios';
        $controller = 'index';
        $action = 'action';
        $controllerClassName = $this->_validateControllerClassName(
            $realModule, 
            $controller
        );            
        // If controller was not found
        if (!$controllerClassName) {
            return false; 
        }            
        // Instantiate controller class
        $controllerInstance = Mage::getControllerInstance(
            $controllerClassName, 
            $request, 
            $this->getFront()->getResponse()
        );
        // If action is not found
        if (!$controllerInstance->hasAction($action)) { 
            return false; // 
        }            
        // Set request data
        $request->setModuleName($module);
        $request->setControllerName($controller);
        $request->setActionName($action);
        $request->setControllerModule($realModule);            
        // Set your custom request parameter
        $request->setParam('url_path', $path[1]);
        // dispatch action
        $request->setDispatched(true);
        $controllerInstance->dispatch($action);
        // Indicate that our route was dispatched
        return true;
    }
    
  2. 在 config.xml 中定义您的自定义路由器:

    <stores>
        <default>
            <web>
                <routers>                               
                    <your_custom>
                        <area>frontend</area>
                        <class>Custom_Portfolios_Controller_Router_Custom</class>
                    </your_custom>
                </routers>
            </web>
        </default>
    </stores>
    
  3. 在 Magento 中享受您的自定义路由:)

于 2010-11-11T19:41:51.803 回答
8

这样做的方法是使用 URL 重写。事实上,您找到的后缀配置可能被 Mage_Catalog 用来创建它的重写集。我是第一次接近这个特殊的功能,所以这个片段应该加一点盐......

// 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();

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

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

于 2010-11-11T13:55:14.517 回答
0

最简单的方法(当您不需要自动生成许多 url 时)是使用内置的 Url Rewrites 模块。转到管理后端 ->目录->网址重写管理并设置您喜欢的任何网址重写。

于 2010-11-11T14:39:35.623 回答