首页 > PHP资讯 > PHP培训技术 > Yaf零基础学习总结Yaf中自定义路由的实现

Yaf零基础学习总结Yaf中自定义路由的实现

PHP培训技术

getConfig();				YafRegistry::set('config', $arrConfig);		//版本信息		$appConfig = new YafConfigIni(APPLICATION_PATH . '/conf/app/appConfig.ini');		YafRegistry::set('appConfig', $appConfig);		//注册参数		$paramsConfig = new YafConfigIni(APPLICATION_PATH . '/conf/app/paramsConfig.ini');		YafRegistry::set('paramsConfig', $paramsConfig);		$adConfig = new YafConfigIni(APPLICATION_PATH . '/conf/data/adConfig.ini');		YafRegistry::set('adConfig', $adConfig);		$channeldataConfig = new YafConfigIni(APPLICATION_PATH . '/conf/data/channeldataConfig.ini');		YafRegistry::set('channeldataConfig', $channeldataConfig);		$cacheConfig = new YafConfigIni(APPLICATION_PATH . '/conf/sys/cacheConfig.ini');		YafRegistry::set('cacheConfig', $cacheConfig);		//print_r($paramsConfig);exit;	}	public function _initParams() {		$paramsConfig=YafRegistry::get("paramsConfig")->paramsField;				foreach ($_GET as $key => $item)			$items[$key]=$item;				foreach ($paramsConfig as $key => $baseParam)					$params[$key]=!empty($items[$key])?$items[$key]:$baseParam;		/*		 * 通过from 获取版本及平台信息		 */		$platformNum = substr($params['from'],-4);		switch ($platformNum){			case '3012':$platform='iphone';break;			case '9012':$platform='ipad';break;			case '5012':$platform='android';break;			case '5014':$platform='androidp';break;			case '4042':$platform='wp';break;			case '4030':$platform='surface';break;			default:$platform='iphone';		}		$version = substr($params['from'], 2, 3); //100 3位		if(!empty($version)){			if($version=='000'){				$version = 'vbase';			}else{				$version = 'v'.$version;			}		}else{			$version = 'vbase';		}		//print_r($version);exit;		/*		 * end		 */		/*		 * 优先 GET version/platform		 */		$params['version']=!empty($_GET['version'])?$_GET['version']:$version;		$params['platform']=!empty($_GET['platform'])?$_GET['platform']:$platform;		//print_r($params);exit;		YafRegistry::set('params', $params);	}	public function _initPlugin(YafDispatcher $dispatcher) {		//注册一个插件		//$objSamplePlugin = new SamplePlugin();		//$dispatcher->registerPlugin($objSamplePlugin);		//$demo = new DemoPlugin();        //$dispatcher->registerPlugin($demo);	}	public function _initRoute(YafDispatcher $dispatcher) {		   		//http://php.net/manual/zh/yaf-route-rewrite.construct.php 				$params 	=	YafRegistry::get("params");		$appConfig	=	YafRegistry::get("appConfig");		$version 	=	!($appConfig->apiVersion->$params['version'])?'vbase':$params['version'];		$platform 	=	!($appConfig->platformLimit->platform->$params['platform'])?'iphone':$params['platform'];		$apiResourceConfig=$appConfig->apiResource;		$apiVersionConfig=$appConfig->apiVersion->$version->$platform;		$apiVersionExtends=!empty($apiVersionConfig->extends)?$apiVersionConfig->extends:'vbase';		$apiVersionCurrent=!empty($apiVersionConfig->current)?$apiVersionConfig->current:'vbase';		$apiResourceEextends=!empty($apiResourceConfig->$apiVersionExtends->$platform)?$apiResourceConfig->$apiVersionExtends->$platform:'';		$apiResourceCurrent=!empty($apiResourceConfig->$apiVersionCurrent->$platform)?$apiResourceConfig->$apiVersionCurrent->$platform:'';		$apiResource['extends']= array(			'version' =>$apiVersionExtends , 			'resource'=>explode('#', $apiResourceEextends)		);		$apiResource['current']= array(			'version' =>$apiVersionCurrent,			'resource'=>explode('#',$apiResourceCurrent)		);		//资源名		$resourceArr=explode("/",$params['resource']);		if(in_array($resourceArr[0], $apiResource['current']['resource'])){			$resource=$apiResource['current']['version'].'_'.$platform.'_'.$resourceArr[0];		}elseif(in_array($params['resource'], $apiResource['extends']['resource'])){			$resource=$apiResource['extends']['version'].'_'.$platform.'_'.$resourceArr[0];		}else{			echo "place check you resource params ";			print_r($params['resource']);exit;		}		if(!empty($params['debug'])&&$params['debug']=='sys'){			echo '路由信息:Version_'.$resource.'
参数信息:'; echo '
';			print_r($params);			echo '
'; } //判断输出内容格式 if($params['platform']=='android'||$params['datatype']=='json'){ header('Content-type: application/json;Charset=utf-8'); }elseif($params['platform']=='iphone'){ header('Content-type: application/x-msgpack;Charset=utf-8'); //Header("Content-type: application/x-msgpack"); Header("Accept-Ranges: bytes"); //Header("Accept-Length: ".strlen($result)); Header("Content-Disposition: attachment; filename=$params[resource].msgpack"); } $config = array( "name" => array( "type" => "rewrite", //YafRoute_Rewrite route "match" => "*", "route" => array( 'controller' => "version_".$resource, //route to user controller, 'action' => !empty($resourceArr[1])?$resourceArr[1]:"index", //todo ), ), ); YafDispatcher::getInstance()->getRouter()->addConfig( new YafConfigSimple($config) ); } public function _initView(YafDispatcher $dispatcher){ //在这里注册自己的view控制器,例如smarty,firekylin //不渲染模板 $dispatcher->disableView(); }}

PHP培训技术

本文由欣才IT学院整理发布,未经许可,禁止转载。
支持3不支持0