Php – How to write Routing Chains for a Subdomain in Zend Framework in a routing INI file


I am trying to create a subdomain using the Zend Router, and then for each section under a subdomain, such as I am creating another route and then trying to chain it to the subdomain route. but I don't know how. I have read all the documentation I could find and all the forums, but it leads me to figure it out on my own. So far, my attempts just get me this error:

Catchable fatal error: Argument 2 passed to
Zend_Controller_Router_Rewrite::addRoute() must implement interface
Zend_Controller_Router_Route_Interface, null given, called in
/var/local/zend/library/Zend/Controller/Router/Rewrite.php on line 155
and defined in
/var/local/zend/library/Zend/Controller/Router/Rewrite.php on line 93

with the following code:

routes.b2b.type = "Zend_Controller_Router_Route_Hostname"
routes.b2b.route = ""
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

I cannot find an example of how to do chaining this with an INI file anywhere on the net. The entire application is written in an INI for the routing config, so I can't switch it over to an array based config (or XML for that matter), in which 100% of the examples on the internet are in.

If I could do it in array form, I could just say this:

$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
        'controller' => 'index',
        'module'     => 'b2b',
        'action'     => 'index'

$hostnameRoute = new Zend_Controller_Router_Route_Static(
        'controller' => 'index',
        'module'     => 'b2b',
        'action'     => 'signup'
    $chainedRoute = new Zend_Controller_Router_Route_Chain();

Does anyone have any ideas on how to do the above in an INI file?

Best Solution

Here's basically what you want, in INI format:

routes.b2b.type = "Zend_Controller_Router_Route_Hostname"
routes.b2b.route = ""
; 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" = "bar" = '[a-z]+'

This will give you the following routes: b2b-signup, and b2b-anotherroute.

Here's some important notes on route chaining:

When chaining routes together, the parameters of the outer route have a higher priority than the parameters of the inner route. Thus if you define a controller in the outer and in the inner route, the controller of the outer route will be selected.

Parent / child chained route names are always concatenated with a dash! So, like in the example above, b2b.chains.signup becomes a route named b2b-signup (which you can use for URL assembly, etc).

You can keep chaining! Chains of chains can have chains.

Children of chained routes do not work with wildcards. See #ZF-6654. Here's blog post that talks about why that may not be a big deal.

Related Question