Yaf zero-basic learning summary implementation of custom routes in Yaf
GetConfig (); Yaf \ Registry: set ('config', $ arrConfig); // version information $ appConfig = new Yaf \ config \ Ini (APPLICATION_PATH. '/conf/app/appConfig. ini '); Yaf \ Registry: set ('appconfig', $ appConfig); // register the parameter $ 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 \ Re Gistry: 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 \ Re Gistry: get ("paramsConfig")-> paramsField; foreach ($ _ GET as $ key => $ item) $ items [$ key] = $ item; foreach ($ paramsConfig as $ key => $ baseParam) $ params [$ key] =! Empty ($ items [$ key])? $ Items [$ key]: $ baseParam;/** obtain the version and platform information through the from statement */$ platformNum = substr ($ params ['from'],-4 ); switch ($ platformNum) {case '000000': $ platform = 'iPhone '; break; case '000000': $ platform = 'iPad'; break; case '2016 ': $ platform = 'Android'; break; case '000000': $ platform = 'androidp'; break; case '000000': $ platform = 'WP '; break; case '000000': $ platform = 'surface '; break; default: $ platform = 'iPhone';} $ version = substr ($ params ['from'], 2, 3 ); // 100 3-bit 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) {// register a plug-in // $ 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); // resource Name $ resourceArr = explode ("/", $ params ['resource']); if (in_array ($ resourceArr [0], $ apiResource ['current'] ['resource']) {$ resource = $ ApiResource ['Date'] ['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 'Route info: Version _'. $ resource. 'parameter info: '; echo'';print_r($params);echo '
';} // Determine the output content format if ($ params ['platform'] = 'Android' | $ params ['ype Ype'] = '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; fi Lename = $ 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) {// register your view controller here, for example, smarty, firekylin // do not render the template $ dispatcher-> disableView ();}}