views:

686

answers:

3

I'm trying to implement route chaining for an admin panel on a Zend Framework site that I am working on. I'm using the following configuration file in hopes that the "admin" route routes with "/admin" and that the "adminLogin" route routes with "/admin/login".

<?xml version="1.0" encoding="UTF-8"?>
<routes>
    <admin>
     <route>admin</route>
     <defaults>
      <module>admin</module>
      <controller>index</controller>
      <action>index</action>
     </defaults>
     <chains>
      <adminLogin>
       <route>login</route>
       <defaults>
        <module>admin</module>
        <controller>login</controller>
        <action>index</action>
       </defaults>
      </adminLogin>
     </chains>
    </admin>
</routes>

With that configuration though, only "adminLogin" works. The route "admin" routes to the default module/controller/action.

I think that I must be missing something with how chaining works. Any feedback greatly appreciated

A: 

This is the wrong use of chains.

Just declare a route for /admin, and a different one for /admin/login. Chains are more useful if you're using more than one route class to determine the resolution of one route (like hostname and path).

Justin
This is actually a really good use of chains. Now, he could change the base url of his entire admin section just by changing the route parameter of the parent chain. That's it, all the rest of the app cares about is the route name.
jason
this is perfectly correct use of chains. and chains can be very useful, even without using more than one route class.
Bittarman
A: 

The parent route of a chain (in this case, admin) will not actually match like a real route. Its sole purpose it the catch the top level match, then let its chained children handle the actual routing.

Try explicitly adding an empty static route to the top of your chain, like so:

<chains>
    <index type="Zend_Controller_Router_Route_Static">
     <route></route>
     <defaults module="admin" controller="index" action="index" />
    </index>
    <login>
     <route>login</route>
     <defaults>
       <module>admin</module>
       <controller>login</controller>
       <action>index</action>
     </defaults>
    </login>
</chains>

See an older answer of mine for some more details and gotchas of chained routes.

Also, it is worth noting the that router automatically concatenates chained route names with a dash, so, if you ever need to use your login route explicitly, it would currently be named admin-adminLogin. I would recommend renaming it to simply login.

jason
A: 

This might not work any more, because of a bug. There is a workaround though.

http://framework.zend.com/issues/browse/ZF-7848

Joust