: This article mainly introduces php framework laravel learning 3 (basic functions). If you are interested in PHP tutorials, refer to it. I. routing
Routing is actually a command used in browsers to send http requests. Many api functions actually send requests through URLs.
Http request methods and responses are basic courses, but there are a lot of content. you should first study them.
Laravel routes are different from other frameworks and need to be manually customized one by one. The advantage is that they are powerful, flexible, and easy to manage.
Three basic methods
First
Is Route: get ('path', function (){...}); In this form, you can change get to post, put, delete, and other request methods as needed. this method is the most common and recommended method;
After routing, you can use closures (anonymous functions) to directly execute a piece of code. this is not available in many frameworks. In reality, it is not possible to write the business code to this small closure, but to put it in the controller, therefore, the most common syntax is Route: get ('path', xxxController @ action );
The closure function of a Route is most useful for testing a function or code, that is, writing and using. it is very convenient. I usually keep a Route: get ('test', function () {...}); Perform the test.
There are often many routes in the project, and some people think it is slow to write one by one. Some CRUD are very standardized, so there is a so-called Restful routing (controller ).
Second:
Route: resource ('posts', 'postscontroller ');
Write such a route to generate seven routes with the route name. the routes of these seven routes are developed according to the restful standard; however, this route has a restriction, that is, in the corresponding controller, except for the 7 methods corresponding to rest, if other methods are to be accessed, you need to define a route before the Restful route to access it.
Third:
It is called a control route, or an implicit route:
Route: controller ('foo', 'foocontroller ');
This method is flexible and uses magic methods to set the http request method on the method name, which is very convenient for some api requests.
Note the following when naming an implicit route because it does not write routes one by one:
Route::controller('users', 'UserController', [ 'anyLogin' => 'user.login',]);
The method name is followed by the route name;
Route Name
What is the purpose of naming a route? It is useful because the routing (url) is used in a large number of View files. with the routing name, the routing can be decoupled and the url can be modified without affecting the url in the view;
Route parameters
The routing parameters we usually call generally look like this index. php? User = ooo & psw = xxx; but in Laravel's view, this plaintext method is ugly and insecure. Therefore, it is written as follows:
Route: get ('user/{name} ', function ($ name) {// you can directly use $ name })
If the value of this parameter is a string and cannot be the same as that of other routes, the system will give priority to the route, so that the value will not be obtained.
The value can be directly transmitted, or the value can be obtained through the object method, Route?> Input ('id ')
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.
The above introduces the php framework laravel learning 3 (basic functions), including some content, hope to be helpful to friends who are interested in PHP tutorials.