Use PHP to implement routes similar to the CI framework.

Source: Internet
Author: User

At the front-end time, I wrote an API about the mobile app, which always uses the QUERY_STRING address and distinguishes all the actions based on an act parameter, which makes the developer seem eye-catching. I wanted to rewrite it as "? C = Controller & M = Method & type = 3 & id = 1 ". The M parameter is used to load and instantiate the file, then we can see that the Sina Weibo API routes the address. It also decided to follow suit to address routing. Originally, the CI framework had its own routing effect, but because it was considered to be an API, it wanted to write purely.

The default controller (INDEX) and method (INDEX) are supported ):

Index. php
Index. php/Controller
Index. php/controller/Method
Index. php/controller/method/prarme1/value1
Index. php/controller/method/param1/value1/param2/value2 .....

<? Phpdefine ('module _ dir ','. /classes/'); $ app_path = str_replace ($ _ server ['document _ root'], '', _ file _); $ se_string = str_replace ($ app_path, '', $ _ server ['request _ URI ']); // calculates the index. index of the field after PHP. PHP/controller/methon/ID/3 $ se_string = trim ($ se_string, '/'); // echo $ se_string. '<br>'; // filter $ se_string. $ Ary_url = array ('controller' => 'index', 'method' => 'index', 'pramers' => array ()); // var_dump ($ ary_url); $ ary_se = explode ('/', $ se_string); $ se_count = count ($ ary_se ); // route control if ($ se_count = 1 and $ ary_se [0]! = '') {$ Ary_url ['controller'] = $ ary_se [0];} else if ($ se_count> 1) {// calculate the following parameters, key-value $ ary_url ['controller'] = $ ary_se [0]; $ ary_url ['method'] = $ ary_se [1]; if ($ se_count> 2 and $ se_count % 2! = 0) {// No key-value form die ('parameter error');} else {for ($ I = 2; $ I <$ se_count; $ I = $ I + 2) {$ ary_kv_hash = array (strtolower ($ ary_se [$ I]) =>$ ary_se [$ I + 1]); $ ary_url [pramers] = array_merge ($ ary_url [pramers], $ ary_kv_hash) ;}}$ module_name = $ ary_url ['controller']; $ module_file = module_dir. $ module_name. '. class. PHP '; // echo $ module_file; $ method_name = $ ary_url ['method']; If (file_exists ($ module_file) {include ($ module_file); $ obj_mo Dule = new $ module_name (); // instantiate the module M if (! Method_exists ($ obj_module, $ method_name) {die ('method does not exist');} else {If (is_callable (Array ($ obj_module, $ method_name ))) {// whether the method can be called // var_dump ($ ary_url [pramers]); $ get_return = $ obj_module-> $ method_name ($ ary_url [pramers]); // execute method A and pass the array of the key-value parameter to if (! Is_null ($ get_return) {// var_dump ($ get_return);} else {die ('this method cannot be called ');}}} else {die ('module file does not exist') ;}?>

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.