当前位置:  首页  >  PHP教程  >  PHP框架专栏  >  yaf

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

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

getConfig();		
		Yaf\Registry::set('config', $arrConfig);
		//版本信息
		$appConfig = new Yaf\Config\Ini(APPLICATION_PATH . '/conf/app/appConfig.ini');
		Yaf\Registry::set('appConfig', $appConfig);
		//注册参数
		$paramsConfig = new Yaf\Config\Ini(APPLICATION_PATH . '/conf/app/paramsConfig.ini');
		Yaf\Registry::set('paramsConfig', $paramsConfig);

		$adConfig = new Yaf\Config\Ini(APPLICATION_PATH . '/conf/data/adConfig.ini');
		Yaf\Registry::set('adConfig', $adConfig);

		$channeldataConfig = new Yaf\Config\Ini(APPLICATION_PATH . '/conf/data/channeldataConfig.ini');
		Yaf\Registry::set('channeldataConfig', $channeldataConfig);

		$cacheConfig = new Yaf\Config\Ini(APPLICATION_PATH . '/conf/sys/cacheConfig.ini');
		Yaf\Registry::set('cacheConfig', $cacheConfig);
		//print_r($paramsConfig);exit;
	}

	public function _initParams() {
		$paramsConfig=Yaf\Registry::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;
		Yaf\Registry::set('params', $params);
	}

	public function _initPlugin(Yaf\Dispatcher $dispatcher) {
		//注册一个插件
		//$objSamplePlugin = new SamplePlugin();
		//$dispatcher->registerPlugin($objSamplePlugin);
		//$demo = new DemoPlugin();
        //$dispatcher->registerPlugin($demo);
	}

	public function _initRoute(Yaf\Dispatcher $dispatcher) {		
   		//http://php.net/manual/zh/yaf-route-rewrite.construct.php 		
		$params 	=	Yaf\Registry::get("params");
		$appConfig	=	Yaf\Registry::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", //Yaf\Route_Rewrite route "match" => "*", "route" => array( 'controller' => "version_".$resource, //route to user controller, 'action' => !empty($resourceArr[1])?$resourceArr[1]:"index", //todo ), ), ); Yaf\Dispatcher::getInstance()->getRouter()->addConfig( new Yaf\Config\Simple($config) ); } public function _initView(Yaf\Dispatcher $dispatcher){ //在这里注册自己的view控制器,例如smarty,firekylin //不渲染模板 $dispatcher->disableView(); } }

吐了个 "CAO" !
扫码关注 PHP1 官方微信号
PHP1.CN | 中国最专业的PHP中文社区 | PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | PHP问答
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved PHP1.CN 第一PHP社区 版权所有