views:

31

answers:

1

I am trying to get the routing information from a random URL (part of my application).

I tried to instantiate a Zend_Controller_Request_Http with an URL but it didn't fill in the controller, module, action fields automatically.

I suppose it should somehow be linked to the Route info but cannot figure how to connect this.

Any leads?

+1  A: 

Its not directly linked. What happens is the router calls its route method passing the request as its argument. It then loops through all the registered routes in reverse order calling the route's match method with the request as an argument - if it matches it sets the parameters on the request before its returned.

The problem is you cant directly call Zend_Controller_Router_Rewrite::route without modifying the current request cycle, so you have to rely on some "fudgery" or reproduce the logic in this method in your own ruter sbuclass or elsehwere.

Example of fudging:

// assume $router is your router instance, $request is the REAL request.

$testRequest = new Zend_Controller_Request_Http($url);

// need to use a try because if the route doesnt match youve got an exception coming
try {
  $router->route($testRequest);
} catch(Zend_Controller_Router_Exception $e) {
  $testRequest = false;
}

// revert back to the real current route which was modified during the previous call

$router->route($request);

if(false !== $testRequest) {
  // consume what you need form testRequest as you normally would
  print_r($testRequest->getParams());
}

I ran into problems with this down the line after i started getting into more complex request lifecyles. I dont remember why but i do remember my solution was to sub class the router and ad a method that route that looked something like this:

public function parseRoute(Zend_Controller_Request_Abstract $request)
{

   $preservedRoute = $this->_currentRoute;
   try {

      $router->route($request);
      $this->_currentRoute = $preservedRoute;

   } catch(Zend_Controller_Router_Exception $e) {

      $this->_currentRoute = $preservedRoute;
      return false;
   }

   return $request;
}

Also keep in mind this is all from memory and it was with 1.6 or 1.7 not the current version so YMMV. Hope that helps.

prodigitalson
Got the idea, thanks for the tip. As my particular app doesn't have complex routing, I guess I'll fudge my way through.
Guillaume Bodi