Zend Framework动作助手Url用法详解
本文实例讲述了ZendFramework动作助手Url用法。分享给大家供大家参考,具体如下:
Url主要用于创建url;
publicfunctionsimple($action,$controller=null,$module=null,array$params=null) publicfunctionurl($urlOptions=array(),$name=null,$reset=false,$encode=true) publicfunctiondirect($action,$controller=null,$module=null,array$params=null)
<?php classIndexControllerextendsZend_Controller_Action { publicfunctioninit() { /*Initializeactioncontrollerhere*/ } publicfunctionindexAction() { //$urlParser=$this->_helper->getHelper('UrlParser'); //var_dump($urlParser->parse('https://www.nhooo.com/article/80479.htm')); $url=$this->_helper->getHelper('Url'); $action='actionname'; $controller='controllername'; $module='modulename'; $params=array('param1'=>'中文参数'); var_dump($url->simple($action,$controller,$module,$params)); $urlOptions=array( 'action'=>$action, 'controller'=>$controller, 'module'=>$module, 'params'=>$params); var_dump($url->url($urlOptions)); var_dump($url->direct($action,$controller,$module,$params)); exit; } }
www.localzend.com/helper_demo1/public/index
string(101)"/helper_demo1/public/modulename/controllername/actionname/param1/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0"
string(101)"/helper_demo1/public/modulename/controllername/actionname/params/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0"
string(101)"/helper_demo1/public/modulename/controllername/actionname/param1/%E4%B8%AD%E6%96%87%E5%8F%82%E6%95%B0"
实现源码如下:
/** *@seeZend_Controller_Action_Helper_Abstract */ require_once'Zend/Controller/Action/Helper/Abstract.php'; /** *HelperforcreatingURLsforredirectsandothertasks * *@usesZend_Controller_Action_Helper_Abstract *@categoryZend *@packageZend_Controller *@subpackageZend_Controller_Action_Helper *@copyrightCopyright(c)2005-2011ZendTechnologiesUSAInc.(http://www.zend.com) *@licensehttp://framework.zend.com/license/new-bsdNewBSDLicense */ classZend_Controller_Action_Helper_UrlextendsZend_Controller_Action_Helper_Abstract { /** *CreateURLbasedondefaultroute * *@paramstring$action *@paramstring$controller *@paramstring$module *@paramarray$params *@returnstring */ publicfunctionsimple($action,$controller=null,$module=null,array$params=null) { $request=$this->getRequest(); if(null===$controller){ $controller=$request->getControllerName(); } if(null===$module){ $module=$request->getModuleName(); } $url=$controller.'/'.$action; if($module!=$this->getFrontController()->getDispatcher()->getDefaultModule()){ $url=$module.'/'.$url; } if(''!==($baseUrl=$this->getFrontController()->getBaseUrl())){ $url=$baseUrl.'/'.$url; } if(null!==$params){ $paramPairs=array(); foreach($paramsas$key=>$value){ $paramPairs[]=urlencode($key).'/'.urlencode($value); } $paramString=implode('/',$paramPairs); $url.='/'.$paramString; } $url='/'.ltrim($url,'/'); return$url; } /** *AssemblesaURLbasedonagivenroute * *Thismethodwilltypicallybeusedformorecomplexoperations,asit *tiesintotherouteobjectsregisteredwiththerouter. * *@paramarray$urlOptionsOptionspassedtotheassemblemethodoftheRouteobject. *@parammixed$nameThenameofaRoutetouse.IfnullitwillusethecurrentRoute *@paramboolean$reset *@paramboolean$encode *@returnstringUrlforthelinkhrefattribute. */ publicfunctionurl($urlOptions=array(),$name=null,$reset=false,$encode=true) { $router=$this->getFrontController()->getRouter(); return$router->assemble($urlOptions,$name,$reset,$encode); } /** *Performhelperwhencalledas$this->_helper->url()fromanactioncontroller * *Proxiesto{@linksimple()} * *@paramstring$action *@paramstring$controller *@paramstring$module *@paramarray$params *@returnstring */ publicfunctiondirect($action,$controller=null,$module=null,array$params=null) { return$this->simple($action,$controller,$module,$params); } }
更多关于zend相关内容感兴趣的读者可查看本站专题:《ZendFrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。