【问题标题】:How do I write Routing Chains for a Subdomain in Zend Framework in a routing INI file?如何在路由 INI 文件中为 Zend 框架中的子域编写路由链?
【发布时间】:2010-11-06 08:38:59
【问题描述】:

我正在尝试使用 Zend 路由器创建一个子域,然后对于子域下的每个部分,例如 subdomain.site.com/section/,我正在创建另一个路由,然后尝试将其链接到子域路由。但我不知道怎么做。我已经阅读了我能找到的所有文档和所有论坛,但它让我自己弄清楚。到目前为止,我的尝试只是给我这个错误:

可捕获的致命错误:参数 2 传递给 Zend_Controller_Router_Rewrite::addRoute() 必须实现接口 Zend_Controller_Router_Route_Interface,null 给定,调用 /var/local/zend/library/Zend/Controller/Router/Rewrite.php 在第 155 行 并定义在 /var/local/zend/library/Zend/Controller/Router/Rewrite.php 在第 93 行

使用以下代码:

routes.b2b.type = "Zend_Controller_Router_Route_Hostname"
routes.b2b.route = "sales.sitename.com"
routes.b2b.defaults.module = b2b
routes.b2b.defaults.controller = index
routes.b2b.defaults.action = index

routes.b2b_signup.type = "Zend_Controller_Router_Route_Static"
routes.b2b_signup.route = "/signup"
routes.b2b_signup.defaults.controller = "index"
routes.b2b_signup.defaults.action   = "signup"

routes.b2b_login.type = "Zend_Controller_Router_Route_Chain"
routes.b2b_login.chain = b2b_signup

我在网络上的任何地方都找不到如何将其与 INI 文件链接的示例。整个应用程序是在路由配置的 INI 中编写的,因此我无法将其切换到基于数组的配置(或 XML 相关),互联网上 100% 的示例都在其中。

如果我能以数组的形式做到这一点,我可以这样说:

$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
    'sales.sitename.com',
    array(
        'controller' => 'index',
        'module'     => 'b2b',
        'action'     => 'index'
    )
);

$hostnameRoute = new Zend_Controller_Router_Route_Static(
    '/signup',
    array(
        'controller' => 'index',
        'module'     => 'b2b',
        'action'     => 'signup'
    )
);
    $chainedRoute = new Zend_Controller_Router_Route_Chain();
    $chainedRoute->chain($b2b_signup)

有人对如何在 INI 文件中执行上述操作有任何想法吗?

【问题讨论】:

    标签: php zend-framework routing subdomain ini


    【解决方案1】:

    这基本上是您想要的,INI 格式:

    routes.b2b.type = "Zend_Controller_Router_Route_Hostname"
    routes.b2b.route = "sales.sitename.com"
    ; you could specify a default module (or anything) to use for the whole 
    ; route chain here, like so: 
    ; routes.b2b.defaults.module = "default"
    
    routes.b2b.chains.signup.type = "Zend_Controller_Router_Route_Static"
    routes.b2b.chains.signup.route = "/signup"
    routes.b2b.chains.signup.defaults.controller = "index"
    routes.b2b.chains.signup.defaults.action = "signup"
    
    routes.b2b.chains.anotherroute.route = "/something/:foo" ; etc, etc.
    routes.b2b.chains.anotherroute.defaults.action = "foo"
    routes.b2b.chains.anotherroute.defaults.controller = "index"
    routes.b2b.chains.anotherroute.defaults.foo = "bar"
    routes.b2b.chains.anotherroute.reqs.foo = '[a-z]+'
    

    这将为您提供以下路线:b2b-signupb2b-anotherroute

    这里有一些关于路由链的重要说明:

    将路由链接在一起时,外部路由的参数优先级高于内部路由的参数。因此,如果您在外部路由和内部路由中定义控制器,则会选择外部路由的控制器。

    父/子链式路由名称总是用破折号连接!所以,就像上面的例子一样,b2b.chains.signup 变成了一个名为 b2b-signup 的路由(你可以将它用于 URL 组装等)。

    你可以继续链接!链链可以有链。

    链式路由的子级不能使用通配符。见#ZF-6654。这是blog post,它谈到了为什么这可能不是什么大不了的事。

    【讨论】:

    • 这正是我正在寻找的答案。自从几个月前我刚开始与 ZF 合作以来,你真的帮了我很多忙,我在互联网上几乎找不到任何东西(我猜直到这篇文章)描述了如何在 INI 中执行此操作。非常感谢!你能推荐一些采埃孚的好书吗?
    • 不,很遗憾。我从来没有读过任何关于 Zend 框架的书(或书?)。也就是说,Rob Allen 的 Zend Framework in Action zendframeworkinaction.com 应该不错。然而,由于采埃孚的发展速度很快,书籍很快就会过时;非常快。例如,关于路由链、Zend_Application 或 Zend_Tool 的任何内容都不会出现在那本书中。
    • “你可以继续链接!链子链可以有链子。”我想知道哪个是正确的方法:1. routes.b2b.chains.anotherroute.chains.yetanotherroute... 2. routes.anotherroute.chains.yetanotherroute...
    • 我相信你的第一个例子是正确的,Kenji。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多