缩短Zend框架路线定义 [英] Shorten Zend Framework Route Definitions

查看:83
本文介绍了缩短Zend框架路线定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Zend Framework中缩短自定义路由的定义?我目前的定义如下:

How can I shorten the definition of my custom routes in Zend Framework? I currently have this as definition:

$route = new Zend_Controller_Router_Route(
    ":module/:id",
    array(
        "controller" => "index",
        "action" => "index" 
    ),
    array("id" => "\d+")
);
self::$frontController->getRouter()->addRoute('shortcutOne', $route);

$route = new Zend_Controller_Router_Route(
    ":module/:controller/:id",
    array("action" => "index"),
    array("id" => "\d+")
);
self::$frontController->getRouter()->addRoute('shortcutTwo', $route);

$route = new Zend_Controller_Router_Route(
    ":module/:controller/:action/:id",
    null,
    array("id" => "\d+")
);
self::$frontController->getRouter()->addRoute('shortcutThree', $route);

是否可以更好地组合这些规则?
在这些放置位置的最佳做法是什么?当前,在Front Controller初始化之后,我就将它们放在了我的引导程序类中。

Is there a way to better combine these rules? And what are your best practices in where to place these? I currently have them in my bootstrap class right after the Front Controller initialization.

推荐答案

在设置这样的路由时,我使用配置文件。作为首选项,我使用XML存储我的配置数据,但是可以很容易地将它们存储为其他受支持的格式。然后,我将路由从配置添加到引导程序中的路由器。

When it comes to setting up routes like this, I use a config file. As a preference, I use XML to store my config data, however these could just as easily be stored in another supported format. I then add the routes from the config, to the router in my bootstrap.

Config:

<config>
    <routes>
        <shortcutone  type="Zend_Controller_Router_Route">
            <route>:module/:id</route>
            <defaults>
                <controller>index</controller>
                <action>index</action>
            </defaults>
            <reqs id="\d+">
        </shortcutone>
        <shortcuttwo  type="Zend_Controller_Router_Route">
            <route>:module/:controller/:id</route>
            <defaults>
                <controller>index</controller>
            </defaults>
            <reqs id="\d+">
        </shortcuttwo>
        <shortcutthree  type="Zend_Controller_Router_Route">
            <route>:module/:controller/:action/:id</route>
            <defaults>
                <controller>index</controller>
                <action>index</action>
            </defaults>
            <reqs id="\d+">
        </shortcutthree>
    </routes>
</config>

引导程序

$config = new Zend_Config_Xml('config.xml');
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addConfig($config, 'routes');

很显然,还有其他选择,我鼓励您阅读文档,但这适合您的示例。

Obviously, there are other options and I'd encourage you to read the documentation on this, however, this fits for your example.

这篇关于缩短Zend框架路线定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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