I'm trying to convert strings to Zend Framework format URLs.
For example, I have a string list
http://example.com/products/category/books
http://example.com/products/category/computers
http://example.com/contact
I want to receive a list with Zend_Controller_Request_Http objects, where parameters like controller, action, params, etc. will be recognized.
Zend_Controller_Request_Http Object ( ... [_params:protected] => Array ( [controller] => index [action] => products [category] => books [module] => default ) ... )
Zend_Controller_Request_Http Object ( ... [_params:protected] => Array ( [controller] => index [action] => products [category] => computers [module] => default ) ... )
Zend_Controller_Request_Http Object ( ... [_params:protected] => Array ( [controller] => index [action] => contact ... )
I found some solution here (thanks Willy Barro)
$url = 'http://example.com/module/controller/action/param1/test';
$request = new Zend_Controller_Request_Http($url);
Zend_Controller_Front::getInstance()->getRouter()->route($request);
$request->getParams();
and it works fine for the first url, but for the rest I receive the same parameters:
[controller]=>index, [action]=>products, [category]=>books
[controller]=>index, [action]=>products, [category]=>books
[controller]=>index, [action]=>contact, [category]=>books
Looks like all I cannot change parameters at all...
Maybe there is some another way to convert string to the zf URL.
Thank you in advance!
So, at the moment I do not find any better solution than this:
$divideURL = function($url){
$zfURL = [];
$request = new Zend_Controller_Request_Http($url);
Zend_Controller_Front::getInstance()->getRouter()->route($request)->getParams();
$zfURL['module'] = $request->getModuleName();
$zfURL['controller'] = $request->getControllerName();
$zfURL['action'] = $request->getActionName();
$url = preg_replace("/".preg_quote($this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost(), "/")."/", "", $url);
$url = preg_replace("/".preg_quote("/".$zfURL['module'], "/")."/", "", $url);
$url = preg_replace("/".preg_quote("/".$zfURL['controller'], "/")."/", "", $url);
$url = preg_replace("/".preg_quote("/".$zfURL['action'], "/")."/", "", $url);
$urlparts = explode("/", $url);
for($i=1; $i<count($urlparts); $i+=2){
if(!empty($urlparts[$i])){
$zfURL[$urlparts[$i]] = $urlparts[$i+1];
}
}
return $zfURL;
};
print_r($divideURL('http://example.com/products/category/books'));
print_r($divideURL('http://example.com/products/category/computers'));
print_r($divideURL('http://example.com/contact'));
Output is:
Array ( [module] => default [controller] => index [action] => products [category] => books )
Array ( [module] => default [controller] => index [action] => products [category] => computers )
Array ( [module] => default [controller] => index [action] => contact )