How PHP routes work

Source: Internet
Author: User
After a while, I suddenly realized that the core of the MVC framework is routing. please kindly advise the specific principle. for example, the TP5 route is very powerful, however, I haven't commented on the source code for two hours. I don't understand what the whole process is like. All I can do now is pathinfo... after a while, I suddenly realized that the core of the MVC framework is routing. please kindly advise the specific principle. for example, the TP5 route is very powerful, however, I haven't commented on the source code for two hours. I don't understand what the whole process is like.

What I can do now is the pathinfo step. the subsequent rules and routes and regular routes cannot be understood. I hope you can give me an implementation process, or a demo or a tutorial on OR, if you have any books, you can also use "O (cost _ cost) O". thank you!

Reply content:

After a while, I suddenly realized that the core of the MVC framework is routing. please kindly advise the specific principle. for example, the TP5 route is very powerful, however, I haven't commented on the source code for two hours. I don't understand what the whole process is like.

What I can do now is the pathinfo step. the subsequent rules and routes and regular routes cannot be understood. I hope you can give me an implementation process, or a demo or a tutorial on OR, if you have any books, you can also use "O (cost _ cost) O". thank you!

In addition to hard-reading, a better way is to open a debugger (such as xdebug) for single-step debugging. On the one hand, you have information from the debugger, on the other hand, you have your own practical use cases (the code you wrote for testing), which makes it easier for you to understand.

Back to the routing topic, you only need to understand how to split and parse pathinfo, it is only a Parsing method to map pathinfo to namespace or map to a controller through regular expressions.

If you want to create a wheel, you can try to implement a simplest method that maps directly to the class under namespace.

Hello/world-> HelloController-> world ()

Test/hello/world-> test \ HelloController-> world ()

After the routes are parsed, they are distributed. you can understand reflection, Closure, and other related knowledge.

Finally, there are many components on github that can only implement the router. you can select the code of those components as read-only compared to the overall framework (you can introduce your project through composer in actual use)

For example:

  • Https://github.com/nikic/Fast...

  • Https://github.com/dannyvanko...

  • Https://github.com/mrjgreen/p...

We recommend a route implementation for you.

Https://github.com/bephp/router

  1. Code is very short, about 100 lines

  2. Supports get, post, and other methods to define routes, as long as they are callable.

  3. In addition to matching the parameters in pathinfo, parameters are automatically obtained through reflection (this feature is useful)

  4. Using a tree structure to store a route ing table is better than several commonly used routes (with beachmark)

  5. Supports "compilation", which is equivalent to caching the route table for faster performance.

  6. You can use composer for installation with no dependency.

Of course, there are simpler and more direct

class R{function($r,$c){$this->r[$r]=$c;} function($r){$this->r[$r]();}}$r=new R;$r->a('/', function(){echo 'hello world';});$r->e('/');

The core idea of MVC is to separate the interface and logic. I personally think it has nothing to do with the front-end routing. the front-end routing is just to unify the entrance.

Do not think about the problem too complicated. In fact, it is to extract the string and then set the number groups, which is actually a line of code.


  '', 1 => 'post', // controller (if the controller exists, require) 2 => 'edit', // method 3 => '123 ', // parameter (use the traditional $ _ GET ['foo'] when obtaining the parameter foo) $ class = $ uri [1]; require 'Controller /'. $ class. '. php '; // controller/post. php $ object = new $ class (); $ action = $ uri [2]; $ parameter = $ uri [3]; echo call_user_func_array (array ($ object, $ action ), array ($ parameter); // call the method in the object and pass the parameter controller/post. php: class post {public function edit ($ id) {return '$ '. _ CLASS __. '-> '. _ FUNCTION __. '('. $ id. ')'; // Output $ post-> edit (1024 )}}

Recently, I wrote a small framework route implementation, which is relatively simple. I think it is okay.
Address: https://github.com/zhoujiangy...
The route under the core folder is the routing implementation class.

The basic idea is that the browser uses a URL string to provide the name of the controller class and method, and PHP finds the corresponding class and method accordingly.

The specific demo code can be pushed here, http://www.jianshu.com/p/d4ce...

Let's look at the ci route. multiple matching modes use the iterator mode to iterate custom routes, iterate out the values that match the SERVER ['request _ url'], that is, retrieve the controller and method. use the new class-> method or call_user_func () to call the corresponding method;

Spend money to see cloud buy tutorials. the official route details are provided.

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.