开发者

Get the route informations from a Zend_Controller_Request

开发者 https://www.devze.com 2023-01-11 14:29 出处:网络
I am 开发者_如何转开发trying to get the routing information from a random URL (part of my application).

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号