0

I have these routes that I need to work:

/user
/user/1
/user/1.json

Currently I have

        'user'              => array('key' => 'user', 'controller' => 'user'),
        'user/:id'          => array('key' => 'user-id', 'controller' => 'user', 'id' => "\d+"),
        'user/:id.json'     => array('key' => 'user-id-json', 'controller' => 'user', 'id' => "\d+"),

It matches the first route for all three urls.

I tried doing this:

        'user'              => array('key' => 'user', 'controller' => 'user'),
        'user/"\d+"'            => array('key' => 'user-id', 'controller' => 'user', 'id' => 1),
        'user/"\d+".json'   => array('key' => 'user-id-json', 'controller' => 'user', 'id' => 1),

but only the first route gets matched still

Is there a way in the Zend Route url rule to "encase" the :id so its picked out and allows me to put something other than / after it?


UPDATED

My bootstrap uses:

public function _initRoutes() {
    $this->addRoutes(array(
        'client'            => array('key' => 'client', 'controller' => 'client'),
        'client/:id'        => array('key' => 'client-id', 'controller' => 'client', 'id' => "\d+"),
        'user'          => array('key' => 'user', 'controller' => 'user'),
        'user/:id'      => array('key' => 'user-id', 'controller' => 'user', 'id' => "\d+")
    ));
}

My extended extender uses

public function addRoutes($routes) {
    $router = Zend_Controller_Front::getInstance()->getRouter();
    foreach ($routes as $pattern => $data) {
        $router->addRoute(
            $data['key'],
            new Zend_Controller_Router_Route(
                $pattern,
                array('controller' => $data['controller'])
            )
        );
    }
}

I tried making addRoutes this:

public function addRoutes($routes) {
    $router = Zend_Controller_Front::getInstance()->getRouter();
    foreach ($routes as $pattern => $data) {
        $router->addRoute(
            $data['key'],
            new Zend_Controller_Router_Route(
                $pattern,
                array('controller' => $data['controller'])
            )
        );
        $router->addRoute(
                $data['key'].'-json',
                new Zend_Controller_Router_Route(
                        $pattern.'.json',
                        array('controller' => $data['controller'])
                )
        );

    }
}

but the latter cancels out /user/50 for example

azz0r
  • 3,283
  • 7
  • 42
  • 85

1 Answers1

0

Firstly, don't try and do this all in one route; you can have multiple named routes that point you at the same place. Its not clear from the chunks if you are trying to define this for one route. If you are, then split your rules into three, and give them the same variables.

If your routes are defined in an ini file (by convention, routes. ini), then I find it easier to have something like /user/:id/:format. You then can either use the Zend contextSwitcher helper in the controller or simply use the :format as a hint to your application. Under the reqs section of your route, you should have (\d+) as Zend_Route looks for sub strings.

Failing that, you might be able to get away with :id:file and define .json as a req for that particular route. This might work if you have to have the file extension. However, as you are kind of fooling the user by serving a PHP script as json, might be better handled at the web server level. If all else fails, and your regular expression foo is up to it, create an instance of the Regex router. While this is untested, this might do it:

/user/(\d+)\.json$
shrikeh
  • 661
  • 9
  • 12
  • Im trying to back support an older api, so having user/50.json is necessary, I cant change it to /user/50/json – azz0r Sep 20 '12 at 15:14
  • Are you suggestion '/user/(\d+)\.json$' => array('key' => 'user-id', 'controller' => 'user', 'id' => "\d+"), – azz0r Sep 21 '12 at 11:47
  • Yes (I think, it's been an age since answering this). – shrikeh Feb 09 '14 at 10:36