如何在magento中为特定url设置控制器


How can I set a controller for a particular url in magento?

我之所以要这样做,是因为我希望两个不同的url使用同一个控制器。即/mymodule/mycontroller/mymodule/mycontroller2应使用相同的控制器Mypackage_Mymodule_Mycontroller

在config.xml中可以做到这一点吗?还是我必须操作路由器?

我更喜欢设置配置文件,而不是操纵路由器。在ZendFramework应用程序中,这可以在routes.ini中完成,如下所示

routes.myroute.route = "/mymodule/mycontroller2"
routes.myroute.defaults.module = "mymodule"
routes.myroute.defaults.controller = "mycontroller"

在马根托有类似的方式吗?

由于SEO的原因,我不想使用重写。

提前感谢

您可以通过以下三种方式进行编辑:

  1. mycontroller2扩展mycontroller,并根据需要覆盖/扩展操作方法,但这似乎不是您想要的。

  2. 使用一些不推荐使用的基于配置的URL重写,它允许您将一个控制器指向另一个控制器(参考Mage_Core_Controller_Varien_Action::_rewrite():

    <global>
        <routers>
            <mymodule><!-- match your routers config node -->
                <rewrite>
                    <mycontroller2><!-- match your controller name -->
                        <override_actions>true</override_actions>
                        <to>mymodule/mycontroller</to>
                    </mycontroller2>
                </rewrite>
            </mymodule>
        </routers> 
    </global>
    

    此外,您还需要创建一个伪控制器操作来处理所有重写(这是这种不推荐使用的重写样式的大问题)。根据您的要求,以下内容应该有效:

    <?php
    class My_Module_MyController extends Mage_Core_Controller_Front_Action
    {
        public function __call($doesnt,$matter)
        {
            //empty...
        }
    }
    
  3. 我刚刚决定,你可以完全跳过配置:

    class My_Module_Mycontroller2Controller extends Mage_Core_Controller_Front_Action
    {
        public function __call($action,$matter)
        {
            $method = preg_split('/Action/',$action);
            $this->_forward(
                $method[0],
                'mycontroller',
                'mymodule'
            );
        }
    }
    

我想您不想使用htaccess重写。所以,为了确保万一你不知道:Magento有一个很好的重写系统内置。很抱歉只发布了一个supid链接,但通过查看它,你可以轻松解决这个任务。。。

http://www.magentocommerce.com/wiki/3_-_store_setup_and_management/seo/how_to_work_with_magento_url_rewrite_rules