Recently, when I was looking at some frameworks, there were some routes that I didn't quite understand. What is the difference between them and the routes in the network? recently, when I was looking at some frameworks, there were routes that I didn't quite understand, what is the difference between this and routing in the network?
Reply content:
I recently read some frameworks, but I don't quite understand them. What is the difference between this and routing in the network?
A route can be said to be a program that processes the relationship between url and function. configure a series of url access rules to provide templates for url access. For example, the python flask framework has the following code:
@app.route('/user')def index(): return 'Hello' @app.route('/')def index(): return 'Home'
In this way, you are accessingYour domain name/user
The requested html template isHello
, AccessYour domain name/
, Which appearsHome
It is the ing between the url and the function or class.
For the framework, it is the process of directing a request path to your target class and method.
As for how to analyze it, there are different implementations and highlights.
It is essentially similar to the network-level routes, but they actually have different levels of things. They have different levels of life.
The routing function is path ing. There are two common URL modes.
www.x.com/a/b/c/d
Orwww.x.com/index.php?a=x&b=y
The routing function maps an address to another address, for examplewww.x.com/x/y.html
<=>www.x.com/index.php?a=x&b=y
When you accesswww.x.com/x/y.html
The access iswww.x.com/index.php?a=x&b=y
In addition to common/
Other symbols can also be used to represent the path, for example:www.x.com/x_y.html
,www.x.com/xy.html
Furthermore,index.php?a=x&b=y
URL exposeda
,b
For the two parameters, use/x/y.html
The form is hidden to play a certain security role.
The essence of the routing in the framework is to convert the URL into a class path.
For examplehttps://segmentfault.com/user/note
The routing function isuser
Class,note
.
Finally, let the framework know that you want to execute the note method of the user class.
There are many identification methods. Different frameworks and url formats have different matching rules.
Sf is relatively simple and can be matched with regular expressions (or even/
You can also make a simple match ).
···········
From the url, use the following rules:/
Split the url. 0th is the domain name, and 1st and 2 are the class names and methods respectively.
Because there is no module name, add the default module 'home'
The class path to be accessed is: Project and directory/module path (linked to the project structure)/Home module/Controller Layer/User Controller
Then, the related classes are automatically loaded, the user controller is instantiated, and the note method is executed.
···········
I used sf as an example. In fact, the framework must have processed some information, which is not that simple.
The routing function is to convert the URL into a class path.