Php 在Magento中生成自定义URL

Php 在Magento中生成自定义URL,php,magento,clean-urls,Php,Magento,Clean Urls,我目前正在尝试使用magento生成自定义URL/路由,目前我已在本地模块的config.xml中设置了默认路由 <frontend> <routers> <portfolios> <use>standard</use> <args> <module>Custom_Portfolios</module&

我目前正在尝试使用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>

标准
定制组合
投资组合
投资组合
这当前适用于/portfolions/index/action/custom字符串的url路径,这是magento的默认路由。 我试图实现的是拥有/portfolions/custom-string.html。我曾尝试使用mod_重写规则,但没有成功,我发现了一些关于使用自定义后缀.html的参考,我已将其添加到同一config.xml文件中

<default><portfolios><seo><portfolios_url_suffix>.html</portfolios_url_suffix></seo></portfolios></default>
.html
我查看了与路由相关的alan storm文档,发现它仅与默认路由路径相关,或者信息有点过时


你知道在magento中控制路由的最佳方法吗?可能有一个简单易懂的相关教程?如果是,请分享:D many

这样做的方法是重写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
,因为它可能是必需的。

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

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

如果不想为每个protfolio项目定义自定义重写,只需执行以下步骤:

  • 编写从Mage_Core_Controller_Varien_router_标准扩展而来的自定义路由器类,并实现
    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;
    }
    
  • 在config.xml中定义自定义路由器:

    <stores>
        <default>
            <web>
                <routers>                               
                    <your_custom>
                        <area>frontend</area>
                        <class>Custom_Portfolios_Controller_Router_Custom</class>
                    </your_custom>
                </routers>
            </web>
        </default>
    </stores>
    
    
    前端
    自定义\u公文包\u控制器\u路由器\u自定义
    
  • 享受您在Magento中的自定义路由:)


  • 我将对此进行进一步调查,您是否知道从xml中的路由引用它的最佳方法,否则我将收到一条404错误消息?抱歉,我不理解这个问题。当我点击公文包/blah.html时,它当前被转发到404错误页,我假设这是因为路由不存在,因此它导致了一个问题,您知道我需要在哪里引用此代码作为xml文件中的方法,还是只需要运行此代码一次?您应该只需要为每个URL运行一次。之后,您可以通过查看Catalog>URL重写管理来确认它的存在。检查它的目标路径是否正是您想要的。我添加了一个
    id
    ,因为我不知道如何处理参数,一个
    id
    将是典型的用法。在我的大脑开始运转后,我想出了如何实现这个系统,非常感谢你的解决方案。嗨,我相信这只有在url已知的情况下才能真正起作用,而不需要手动预先创建数千条规则。谢谢你的建议。我发现上面的解决方案适合我的需要,因此我没有使用这个。感谢您花时间回答这个问题。别忘了为请求设置路由名称-它用于布局句柄:$request->setRouteName('someRouteName'))