Laravel routing Use (ii)

Source: Internet
Author: User

We have described the use of some simple routes in Laravel in the first article.

So let's recap.


1, Base routing

2, multi-Request routing

3, Route pass parameters

4, Routing Restrictions

5, Controller routing


With the foundation of the first article, we can learn the following points of knowledge


However, after the last article, there may be people who have opinions, why?


With regard to the route definition for Laravel, many people see a doubt after that:


Is it tiring to define a route for each address rule?


The problem is really a problem, but Laravel gives us a compromise----resource routing.


Resource Routing


Controller routing I think the main problem is to solve the complexity of routing, because the large-scale application business complex, the controller is quite much, we can not each controller method to define a route. So, the resource routing can help us solve the problem perfectly. But before we solve the problem, let's look at resource routing and what is a resource (RESTful);

RESTful is a design idea, a universally accepted norm.

The word rest is from Roy Thomas Fielding in his 2000 doctoral dissertation. Fielding is a very important person who is the main designer of the HTTP protocol (version 1.0 and 1.1), one of the authors of the Apache server Software, and the first president of the Apache Foundation. As a result, the publication of his paper has attracted much attention and has had a profound impact on the development of the Internet. Fielding his architectural principles of Internet software, named rest, the abbreviation of representational state transfer. My translation of this phrase is "presentation layer State transformation". If a schema conforms to the rest principle, it is called a restful architecture. The best way to understand the restful architecture is to understand what the phrase representational state transfer means, and what each word represents. If you understand the name, it's easy to see what kind of design rest is. We must read this article, understand the restful architecture, the article is very clear about the restful, this article will not repeat the boil.

Laravel's resource controller makes it painless to build RESTful controllers around resources, for example, you might want to create a controller in your app that handles HTTP requests for picture storage, Make:controller with the Artisan command, We can quickly create such a controller:


Of course, we'll talk to you in detail. Use of artisan tools


PHP Artisan Make:controller Photocontroller


The Artisan command will generate a controller file app/http/controllers/photocontroller.php , which contains each

The method that corresponds to the resource operation. Next, you can register a resource route for the controller:



Route::resource (' photo ', ' photocontroller ');


This route declaration contains multiple routes to handle the RESTful action of the picture resource, and accordingly, the controller generated by the Artisan has set up a corresponding processing method for these actions.


We can open the cmd command line and come to the root directory where we place the Laravel framework input PHP artisan route:list will display our routing information, including the corresponding request method for each route, as follows:

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/8B/B9/wKioL1hXPoOSUbnpAADiaV8Re4Y721.png-wh_500x0-wm_3 -wmp_4-s_3367768186.png "title=" 123.png "alt=" Wkiol1hxpoosubnpaadiav8re4y721.png-wh_50 "/>

The same thing. Under our controller, we can see that we have prepared a variety of methods, and the corresponding, from this perspective, resource routing can avoid the situation we are worried about.


Also, we can define some resources through resource routing, such as:

Defining Partial resource routing


Route::resource (' photo ', ' photocontroller ', [' only ' + = [' Index ', ' Show ']]); What does that mean in the Photocontroller controller? To use the index and show methods we can also define the same as the above results route::resource (' photo ', ' photocontroller ', [' except ' = = [' Create ', ' Store ', ' Update ', ' destroy ']); only the meaning that only and except want to express



Named Resource routing


By default, all resource controller actions have a route name, however, we can override these default names by passing in the names array:



Route::resource (' photo ', ' photocontroller ', [' names ' = [' create ' = ' photo.build ']]);


All of the above is the resource routing so just contact Laravel framework may not understand, there is no relationship, you can skip this section to learn the following, back to see this


Routing grouping


For example, a very simple controller route:


Route::get (' Admin/login ', ' admin\[email protected] ');


The meaning of this route we can access Http://www.itbool.com/admin/login can access the GetLogin method in the Usercontroller controller under the admin module , This one can all be understood by the first article.


So what is routing grouping??

For example, I want to write the login and exit page under the Admin module, then we need some of the following routes when considering the way of access:


Route::get (' Admin/login ', ' admin\[email protected] '); Route::p ost (' Admin/login ', ' admin\[email protected] '); Route::get (' Admin/register ', ' admin\[email protected] '); Route::get (' Admin/register ', ' admin\[email protected] '); Route::get (' admin/logout ', ' admin\[email protected] ');


In this way we write a lot of code, and have a lot of the same place, so how to solve it? Very simple, routing grouping


First of all, we note that the action Admin/login Admin is the route prefix prefix to represent

Again notice

Admin\usercontroller Admin is the controller namespace, with namespace to express, we can directly put these same things to omit it, the answer is yes

Then grouped down is:



Route::group ([' prefix ' = ' admin ', ' namespace=>admin '],function () {route::get (' login ', ' [email protected] '); Route::p ost (' Login ', ' [email protected] '); Route::get (' register ', ' [email protected] '); Route::get (' register ', ' [email protected] '); Route::get (' logout ', ' [email protected] ');


Was it a surprise to see the result? Just give it a routing prefix and namespace to declare, so that people understand it?


I believe that the students who understand the database can know that the table prefixes in the database can be specified, meaning the same


Named routes


Named routes make it easy to generate URLs or redirect to a specified route, specify a route name when defining a route, and then use the array key as to specify a route alias:



Route::get (' User/profile ', [' as ' + ' profile ', function () {//}]); The above is done with an anonymous function, then how to use it in the controller: Route::get (' user/ Profile ', [' as ' + ' profile ', ' uses ' = ' [email protected] '];



Routing Grouping & named routes


If you are using routing groupings, you can specify the AS keyword in the routing grouping attribute array to set a common routing name prefix for the routes in the grouping:



Route::group ([' as ' = ' admin:: '], function () {route::get (' dashboard '), [' as ' = ' dashboard ', function () {///route is named "Admin::d Ashboard"}]);



So in the Laravel framework, the route is like this, through the first and second laravel in the route we can basically grasp the


This article is from the "Boolean Education" blog, please be sure to keep this source http://zixueit.blog.51cto.com/12347715/1883879

Laravel routing Use (ii)

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.