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

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化