dongyanzhui0524 2016-09-28 11:50
浏览 51
已采纳

从字符串中获取Zend Framework URL

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!

  • 写回答

1条回答 默认 最新

  • dongpa3109 2016-09-28 15:26
    关注

    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 ) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?