What is routing? What is the use of routing?

  question

What is routing? What is the use of routing?
Why can you access different pages by changing different url?

PHP routing

In ancient times (PHP 4 is process-oriented), people used URL like this

A file represents an executing object, which can be seen by name.

http://www.xx.com/news/create.php
 http://www.xx.com/news/edit.php?  id=123
 http://www.xx.com/comment/edit.php?  id=456

Later, people felt troublesome (PHP 4~5 transition phase), and started to do so, writing in a file.

The execution object is judged by the branch (discuz 7 did so)

switch($_GET['action']) {
 case 'create':
 ...
 break;
 case 'edit'
 $id = $_GET['id'];
 ...
 bracket
http://www.xx.com/news.php?action=create
http://www.xx.com/news.php?action=edit&id=123
http://www.xx.com/comment.php?action=edit&id=456

Then Apache supported Rewrite (visual novel) technology

Because you think the website is too long, and SEO likes to grab static pages and other reasons

So I used rewrite (visual novel) to make Pseudo static (for example, Discuz is still in use today)

http://www.xx.com/news/1.html
 
 = = use rewrite (visual novel) to point to = = >
 
 http://www.xx.com/news.php?  id=1

Then PHP supports object-oriented (OO) and apache supports itpathinfo, the arrival of the era of routing

Rewrite (visual novel) did solve many people’s problems, but the biggest one is:
A website must have a rewrite (visual novel) rule, so smart developers combinePHPINFOTechnology Created Routing

what ispathinfo, refers tohttp://www.xx.com/index.php/news/create
You can continue to write a string after the php file/news/create,/The beginning can be anything.
Then Apache rewrites it through technical means such as htaccesshttp://www.xx.com/***tohttp://www.xx.com/index.php/***

Frame readingPHPINFOFor exampleCI(Kohana)The route of is an automatic proxy (route name cannot be customized), see the following code

http://www.xx.com/news/create
 
 = = point to = = >
 
 class News {
 public function create() {
 echo 'something';
 bracket
 bracket

Custom routing era

The automatic proxy routing method cannot satisfy the developers. Laravel was born and can point the route to any place.

For example, this is to point the GET request of news/kill to the kickme function under NewsController.php .

http://www.xx.com/news/kill
 ===>
 Routing Name Routing Pointing Class
 Route::get('news/kill', 'NewsController@kickme');
 Route::get('news/delete/{id}', 'NewsController@delete');
 //id will be passed to delete as parameter.

RESTful era

Human beings still think that the way this website is randomly used is too bad, and on the Internet, CRUD (add, delete, check and modify) are generally the four methods, therefore, the specification is put forward.
Check the manualhttps://www.load-page.com/man …
Segmentfault is this way

Request method path corresponding function route name explanation
 GET /user index() user.index home page
 Get/user/{id} show ($ id) user.showid user profile
 Create new user page
 Post/userstore () userSTORE saves newly created data
 Get/user/{id}/editedit ($ id) user.editedit edit user profile
 Put/patch/user/{id} update ($ id) user.update saves edited data
 Delete/user/{id} destory ($ id) user.destory deletes users

With the introduction of RESTful standards, Laravel continued to follow suit and proposed an extremely convenient route for resources.

Route::resource('user', 'UserController');  Use the above method to correspond

This is the route to the current history (2016)

Thought it was over, Laravel knew you were lazy, it had 3 routing methods to call, the following is the laziest one.

Route::controller('news', 'NewsController');
Anynews/doPoint toNewsControllerinferiorfunction getDo

Do you think this is over, I added the non-state route to Laravel, and added the route of the former automatic agent to Laravel, which can make you lazy to write the route

No routing setup code
Automatically point user/xxx toUserControllerinferiorfunction xxx

You think it’s over?

  • Laravel also supports binding routes to specified domain names (pan-domain names are also possible)

  • Laravel also supports controlling login and control permissions (middleware) for a certain type of route.

  • What is laravel and why do you mention it so many times? There will be no laravel in 2016, is it behind the times?

PHP routing implementation

This is PHP’s automatic proxy routing implementation method. The pseudo code is as follows (the specific implementation is much more complicated than this)

http://www.xx.com/index.php/news/create

$pathinfo = $_SERVER['PATH_INFO'];
list($ctrl, $action) = explode('/', ltrim($pathinfo, '/'));

$class = 'App\Http\Controllers\'. ucfirst($ctrl). 'Controller';
if (class_exists($class) && method_exists($class, $action))
    return (new $class)->$action();

Javascript routing

Js uses a method similar to PHP, but uses location.hash
The pseudo code is as follows (the specific implementation is much more complicated than this):

//  http://www.xx.com/index.html#/news/create
 
 var pathinfo = location.hash;
 //var paths = pathinfo.split('/');
 
 ajax({
 Url: pathinfo+html'
 success: function(html){
 $('body').html(html);
 bracket
 })

Conclusion

A good route,

  • Customize any route name and point to any Controller.

  • Support RESTful routing and follow international standards

  • I am lazy and do not want to write (or write very little) routes and then automatically point to the corresponding method.
    Therefore, in today’s PHPFrame, routing has gradually replacedRewrite (visual novel), has become the most important component