11

I am trying to extract the GET parameters from a ZF REST URL. It's not the current request and I don't want to call the URL or execute the route, I just need the parameters. I'm looking for a utility function like parse_url(), but for the Zend REST format. Is there one, or do I have to reinvent the wheel?

I've tried a few things like creating a new Zend_Controller_Request_Http but the parameters aren't being populated. It is a valid HTTP URL.

Edit: Upon request, a sample Zend URL:

http://localhost/index/index/param1/foo/param2/bar

So I am just trying to get param1 and param2 out of this URL.

Edit #2: I tried this but it does not seem to work:

$request = new Zend_Controller_Request_Http('http://localhost/home/test/param1/foo/param2/bar');
$front = Zend_Controller_Front::getInstance();
$route = new Zend_Rest_Route($front);
var_dump($route->match($request));
0

2 Answers 2

8

How about $uri = Zend_Uri::factory( $yourUrl )' ? See Zend_Uri.

Edit:
Ah, I see what you mean now. In that case I believe you should try what Gordon suggested. Run your url through the match method of your route.

There probably is a way to retrieve the route from the router with something like (not sure though):

$route = Zend_Controller_Front::getInstance()
                              ->getRouter()
                              ->getRoute( 'theRouteName' );

And then do semething like:

$params = $route->match( $yourUrl );

.. which should give you an array of parameters.

Sign up to request clarification or add additional context in comments.

3 Comments

That gives me a Zend_Uri object, but then what do I do with that object to get the parameters? getQuery() returns false because it only looks for a standard query string (?param1=foo&param2=bar).
Awesome. I just changed getRoute($name) to getCurrentRoute() and this solution works for me. Thanks so much!
+1. deleting mine, upvoting here. this is much more straightforward. Just a minor sidenote: match() will internally replace $yourURL with the current request object if it is not an instance of Zend_Controller_Request_Http to start with.
8

If anyone came here trying to get all params (including module/controller/action) from a stored URL considering the routes defined on your routes.ini, you should:

/**
 * Code kept big just for example purposes
 * Creates a request object, route and injects back the properties parsed
 */
$url = 'http://www.site.com/module/controller/action/param1/test';
$request = new Zend_Controller_Request_Http($url);
Zend_Controller_Front::getInstance()->getRouter()->route($request);

// Module name
$request->getModuleName();

// Controller name
$request->getControllerName();

// Action name
$request->getActionName();

// All parameters
$request->getParams();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.