Framework url Parsing method-PHP source code

Source: Internet
Author: User
Framework url parsing jumps

_ Module = ''; $ this-> _ controller = C ('defaultcontroller'); $ this-> _ action = C ('defaultaction '); $ this-> _ rules = C ('urlmanager => rules');}/*** get model name **/public function getModule () {return $ this-> _ module;}/*** get controller name **/public function getController () {return $ this-> _ controller ;} /*** get action name **/public function getAction () {return $ this-> _ action;}/*** sets the array of request parameters, resolve second-level domain name **/private function parse () {// first check second-level domain name $ modul Es = C ('modules'); if ($ modules) // check {$ host = $ _ SERVER ['http _ host'] only when the module is set; if (C ('domain ') & $ pos = strpos ($ host, C ('domain') {$ preffix = substr ($ host, 0, $ pos-1); if ($ pos = strrpos ($ preffix ,'. ') {$ preffix = substr ($ preffix, $ pos);} if (in_array ($ preffix, $ modules )) {$ this-> _ module = $ preffix; }}}/*** resolve common parameters **/private function parseParam ($ controllerObj) {$ num = count ($ this-> _ requestParam); if ($ num = 0) return; For ($ I = 0, $ num = count ($ this-> _ requestParam); $ isetParam ($ this-> _ requestParam [$ I], $ this-> _ requestParam [$ I + 1]);}/*** url parsing rule * 1. 'variable/varialbe: regexp> '=> 'Controller/action', * 2. 'variable/variable' => 'Controller/action', * 3. 'variable/variable/: varialbe | regexp '=> 'Controller/action', * 4. 'variable/: variable | regexp/varialbe '=> 'Controller/action', * 5. 'variable/variable/* '=> 'Controller/action', * des C: starts with a variable. | the subsequent regular expression is of the variable matching type. if not, it is an arbitrary variable **/private function rule () {$ parseParam = array (); $ requestParamNum = count ($ this-> _ requestParam); foreach ($ this-> _ rules as $ rule => $ path) {$ ruleParam = explode ('/', $ rule); $ ruleParamNum = count ($ ruleParam); if ($ ruleParam [$ ruleParamNum-1] = '*' & $ requestParamNum> = $ ruleParamNum-1) | $ requestParamNum ==$ ruleParamNum) // rule meets the initial conditions {foreach ($ ruleParam as $ key => $ valu E) {if ($ value = '*') {break;} if (substr ($ value, 0, 1) = ':') {$ value = substr ($ value, 1); if (strpos ($ value, '|') {list ($ value, $ regexp) = explode ('| ', $ value); if (! Preg_match ("/$ regexp/", $ this-> _ requestParam [$ key]) {$ parseParam = array (); break ;}} $ parseParam [] = $ value; // parameter name $ parseParam [] = $ this-> _ requestParam [$ key]; // parameter value} else if (strcasecmp ($ value, $ this-> _ requestParam [$ key]) {$ parseParam = array (); break ;}} if ($ parseParam) {$ pathParam = explode ('/', $ path); break ;}} if ($ parseParam & $ key = $ ruleParamNum-1) // match {if ($ ruleParam [$ ruleParamNum-1] = '*'&& $ RequestParamNum >=$ ruleParamNum) {for (; $ key_requestParam [$ key] ;}$ this-> _ requestParam = array_merge ($ pathParam, $ parseParam ); unset ($ parseParam) ;}/ *** execution controller method **/public function run () {$ this-> parse (); if ($ _ SERVER ['request _ URI ']) {$ this-> _ requestParam = explode ('/', trim ($ _ SERVER ['request _ URI'], '/'); if ($ this-> _ rules) // There are routing rules {$ this-> rule (); // this request meets the routing rules} if ($ modules & $ this-> _ module = ''& in_arr Ay ($ this-> _ requestParam [0], $ modules) {$ this-> _ module = $ this-> _ requestParam [0]; array_shift ($ this-> _ requestParam);} // print_r ($ this-> _ requestParam); $ controllerPath = APP_PATH.DS. ($ this-> _ module? 'Modules '. DS. $ this-> _ module. DS :''). 'controllers '. DS; if (file_exists ($ controllerPath. $ this-> _ requestParam [0]. 'controller. class. php ') {$ this-> _ controller = $ this-> _ requestParam [0]; array_shift ($ this-> _ requestParam );}} $ controllerClass = $ this-> _ controller. 'controller'; import ($ controllerClass, $ controllerPath, 'class'); $ controllerObj = new $ controllerClass ($ this); $ this-> _ requestParam [0]; if ($ this-> _ reque StParam [0] & method_exists ($ controllerObj, $ this-> _ requestParam [0]. 'action') {$ this-> _ Action = $ this-> _ requestParam [0]; array_shift ($ this-> _ requestParam );} $ this-> parseParam ($ controllerObj); // try {$ action = $ this-> _ action. 'action'; $ controllerObj-> $ Action ();} catch (Exception $ e) {$ this-> rediretUrl ('/');}} /*** url redirection **/public function rediret ($ url) {header ('Location :'. $ url);}/*** url generation method, which requires reverse resolution Rule **/public function url ($ controller, $ action, $ param, $ module = '') {$ urlPath = $ controller. '/'. $ action; if ($ module) $ urlPath = $ module. '/'. $ urlPath; $ parseParam = array (); if ($ this-> _ rules) {$ ruleArr = array (); foreach ($ this-> _ rules as $ rule => $ path) {if ($ path = $ urlPath) {$ ruleArr [] = $ rule ;}} if ($ ruleArr) {foreach ($ ruleArr as $ rule) {$ ruleParam = explode ('/', $ rule); foreach ($ ruleParam as $ key => $ valu E) {if (substr ($ value, 0, 1) = ':') {$ value = substr ($ value, 1); if (strpos ($ value, '|') {list ($ value, $ regexp) = explode ('|', $ value);} else {$ regexp = '';} if (isset ($ param [$ value]) & (! $ Regexp | preg_match ("/$ regexp/", $ param [$ value]) {$ parseParam [] = $ param [$ value]; unset ($ param [$ value]);} else {$ parseParam = array (); break;} else {$ parseParam [] = $ value ;}} if ($ parseParam) break;} // foreach ($ ruleArr as $ rule) if ($ parseParam) $ urlPath = implode ('/', $ parseParam );} // if ($ ruleArr)} // if ($ this-> _ rules) if ($ param) {foreach ($ param as $ key => $ value) {$ urlPath. = '/'. $ key. '/'. $ value ;}} return $ urlPath ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.