The slag's got to be Laravel. (2)--http routing [1]

Source: Internet
Author: User

A few days ago, and then almost two weeks did not post blog. Formally returned today. Ha ha

1. Routing

When it comes to routing at the time of learning to me puzzled for a few days,. Did not look closely at the document. Then a blind face to use postman[Google plug-in] test the routing method. And then it was strange that composer downloaded Laravel why the only get routing method that was tested in postman was used, similar to this:

Laravel routing in app/http/routes.php This file (Chinese document: dot me )

Added a Get method inside the routes.php

1 route::get (' test ',function() {2     return ' 123 '; 3 });

Then in the postman inside the test can output 123;

Then I add the Post method again. Found in the postman inside the test will be error.

1 Route::p ost ('/test ',function() {2     return 123; 3 });

  

Later added to the Laravel College of the group asked to know,(laravel 5.2.26 After all the routes default to go Web middleware, so no token must not access) Popular saying is to prevent csrf (cross-site) attacks. If you want to test with postman, you need to close csrf. Or you can use form testing to avoid attacks. I'm going to test these routes with the form that the official document gives ...

1Route::get (' Test ',function(){2     $csrf _field=Csrf_field ();3     $html= <<<GET4<form method= "POST" action= "/test" >5{$csrf _field}6<input type= "Submit" value= "test"/>7</form>8 GET;9     return $html;Ten }); OneRoute::p ost (' Test ',function(){ A     return' Success '; -});

This can be seen in the browser (ignore tags, too lazy to hit the horse)

Metch/any routes are similar. And then I'm not going to test a few other routes. Form delivery is only get and post. If you want to see other routes. Currently only know---to close csrf with postman to see the effect. How to close the above CSRF link is described. or app/http/kernel.php comment out line 31st

1 \app\http\middleware\verifycsrftoken::class// Comment I

1.1. Routing Parameters

1 function ($postId$commentId) {2     return $postId. $commentId ; 3 });

Optional Parameters:

1 function ($postId$commentId=null) {2     return ' $postId----'. $postId. ' $commentId------'. $commentId ; 3 });

This means that the {post} parameter is received as PostID. The optional parameter comment can then be either empty or not empty. And then output the corresponding parameter: The following example is to set the optional parameter without giving him a value. Then the output will not have parameters out. If the setting is the reverse. {Post} cannot be set to optional parameter, if set, then error, because it really does not know the value behind comments.

Regular constraint parameters:

1 function ($id$name) {2   return $id. ' ---‘. $name ; 3 })->where ([' id ' = ' = ' [0-9]+ ', ' name ' = ' [a-z]+ ']);

The parameter ID of this input must be a number. Name must be a-Z or multiple a-z+---that is, the regular constraint of both parameters.

Output:

Part of the content reference: Laravel 5.2 Chinese documents

The slag's got to be Laravel. (2)--http routing [1]

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.