Recently began to learn laravel, feel great. In order to share what I have learned, I have established a blog, Welcome to visit: Www.insp.top. Because I am also a beginner, there will certainly be many omissions, I hope you can comment on the common progress.
Original address: WWW.INSP.TOP/ARTICLE/6
Learning Laravel must learn its route, routing is laravel characteristic, is a most important. It is the first stop to experience laravel elegance and beauty.
This route is not the same route, and does not confuse routing with routers, although they all have the effect of distributing requests.
In this case, routing is the analysis of the Uniform Resource Identifier (URI) from the client request, which distributes the request to the expected processing logic according to the set rules, which is the routing rule, which is the route.
Let's say we have this simple process logic:
echo ' Hello, world ';
I want to access this process logic through the URL http://yourdomain/host, and our routing rule should be/host, which accepts the request type is get. How to achieve it in laravel. We add the following code by editing the app/http/routes.php in the Laravel Framework directory:
Route::get (' Host ', function () {
echo ' Hello, world ';
});
That's what we thought before. With such a simple example, we should at least understand what the route is doing, in fact, to match the request address. Many people are not used to it because the previous framework has given you a set of rules, and now, this option is all about you, you have the highest level of custom rules. So, it's not trouble, it's more flexible. Let us continue to understand that you will fall in love with him. Summary
Laravel's routing gives us more reason to customize the rules. I'm not used to this, I'm a beginner from thinkphp, accustomed to thinkphp default routing rules, Http://yourdomain/Module/Controller/Action, Such routing rules allow you to write only the controller, and if you need customization or streamlining, you can adjust and modify the routing rules through the configuration file.
While acknowledging that the TP framework and other similar frameworks can still be call configured to implement the same flexible routing rules, but the unavoidable thing is that you need to arrange the controller, the model in his rule system, who knows that the base class of these controllers is a mixture of methods from other classes, highly coupled, A significant increase in additional overhead, the scalability is not very good or the expansion is too cumbersome.
Conversely, if you're just a simple program, Laravel can create a simple, dynamic program with a flexible route customization, without even having to create a standard controller.
Why is the Laravel controller not similar to the structure of TP? Because the Laravel controller is for organizing a class of behavior, or for a resource to establish a standard resource controller. The significance of TP and CI controllers becomes more important, which is the main component of logic in the whole framework. Laravel implementation of the main logic can be a series of class libraries, simple logic can even directly in the routing implementation, and the controller is only one way to implement. Laravel This design is more scientific in complex projects, making layered systems very easy to implement. a few things you have to know
Learning Laravel Routing, there is the same foundation absolutely cannot throw--http Protocol Foundation. The HTTP protocol is not required to be fully understood, but a small number of concepts must be known. If you don't understand it, there's a lot of confusion when you read the Laravel document. I'll talk a little bit here.
HTTP request process as shown above, the client sends an HTTP request message to the server, and the server responds to the corresponding request.
There are a number of ways to request a client, often with Get,post. Generally speaking, we are in the browser through the input URL to return, is the way to request data, and through the form submission, is generally submitted by POST. For the request method, I only stay at this level before I learn laravel, but now, you have to know that in addition to get,post, there are put, DELETE and so on.
In addition to the request address, there are requests for host and resources, the request host is generally full domain name and IP address, such as www.a.com or 142.53.75.112, resource is actually a legitimate Uniform Resource Identifier (URI), relative to the directory of service processing, such as/index.php,/ Article/1.
The above part is the request header of the HTTP protocol, if you are requesting by POST and put method, the request body is also included, the request body is the specific data.
We should go to understand RESTful, this is a design idea, this to learn Laravel route has a great help, especially in learning Laravel controller part of the resource controller, a lot of people's confusion is not understand RESTful in the case of the generation. Nanyi's log has two detailed explanation of RESTful, I will no longer swim, one of the portal in this given: http://www.ruanyifeng.com/blog/2011/09/restful.html officially began to understand Routing of Laravel
The following article defaults to the view that the reader has a general understanding of the RESTful and HTTP protocols, and that the nouns that appear are no longer annotated.
Laravel's routing flexibility is really a problem at some point, but I understand that this is not a waste of time, it saves most of the development time, and improves the development efficiency.
We define the route:
Route::get ('/', function () {return
view (' index ');
});
means that our access to http://yourdoamin/will display the content of the rendered view file index. This is very concise for making a home page, and no additional controller is needed. Of course, if your program, the display of the home page is a class of organizational methods, can also be implemented with the controller, about the controller I will say later.
If I want to access my blog's about pages through address http://yourdomain/about, the page is defined in the view about file, how to define the route.
Route::get (' About ', function () {return
view (' about ');
});
So now my blog has a bunch of articles, I want to access the article with ID 1 through the address http://yourdomain/article/1, through the HTTP://YOURDOMAIN/ARTICLE/2 Access ID 2 article how to do.
Route::get (' Article/{id} ', function ($id) {return
' article: '. $id;
});
However, we all know that in most cases, IDs can only be numbers, and how we can guarantee that we get only numbers. The others will be ignored. This definition can be:
Route::get (' Article/{id} ', function ($id) {return
' article: '. $id;
}) ->where (' id ', ' [0-9]+ ');
By this definition, if you enter an address that is http://yourdomain/article/a, it will not be matched to that route. We can easily see that this is through regular rules, that is, we can make the necessary matching rules according to the situation.
However, if there is such a situation, there are many places need ID as a parameter, most of the ID is this number, we certainly do not want each routing rule to write a where method to set the ID matching rules. This can be accomplished by using a method such as:
Route::p attern (' id ', ' [0-9]+ ');
In this case, {ID} appears within each routing rule, and the rule will match. LARAVEL5 official documentation suggests that the above content be defined in the boot method in the Routeserviceprovider, by means of an official document.
Read here, you should have a concept of creating a simple route. This article will not be a comprehensive listing of all the content of the official documents, but in the easy to confuse the specific point of the detailed explanation. It is recommended to read the official document and try the examples given here. Know that your computer and server will not burst when you try.
In addition to defining a route to match a GET request by Route::get (), you can define a route to handle POST, put, and DELETE, respectively, Route::p Ost (),