1.HTTP Routing
Referring to the document, all routes are defined in the app/http/routes.php file loaded by the App\providers\routeserviceprovider class.
1.1 Basic Routing
Route::get ($uri,$callback); Route::p OST ($uri,$callback); Route::p UT ($uri,$callback); Route::p Atch ($uri,$callback); Route::d elete ($uri,$callback); Route:: Options ($uri,$callback); Route:: Match ([' Get ', ' post '], '/',$callback);//response Get, POST requestRoute::any (' foo '),$callback);//respond to all requests
Where the URI is relative to the project root, the url,callback is either a closure or a controller action.
If you are interested in the differences between URIs and URLs, you can refer to http://www.cnblogs.com/gaojing/archive/2012/02/04/2413626.html
1.2 Routing parameters
//Single route parametersRoute::get (' User/{id} ',function($id) { return' User '.$id;});//Multiple route parametersRoute::get (' Posts/{post}/comments/{comment} ',function($postId,$commentId) { //});//Single route parameter (optional)Route::get (' User/{id?} ',function($id= 1) { return' User '.$id;});//Multiple route parameters (optional)Route::get (' Posts/{post}/comments/{comment?} ',function($postId,$commentId= 1) { //});//Note: When multiple parameters are available, only the last parameter can be set as optional, and other location settings can optionally parse the error//regular constraint for a single parameterRoute::get (' User/{name?} ',function($name= 2) { return $name;})->where (' name ', ' \d+ ');//constraint parameter is number//regular constraint multiple parametersRoute::get (' User/{id}/{name} ',function($id,$name) { //})->where ([' id ' = ' = ' [0-9]+ ', ' name ' = ' [a-z]+ ']);2. Create a controller 2.1 manually created
<? phpnamespace app\http\controllers; Use App\user; Use App\http\controllers\controller; class extends controller{ publicfunction index ($id) { return $id; }}
Then, in routes.php, define the route that points to the controller action
Route::get (' User/{id} ', ' [email protected] ');
2.2 Artisan Method Creation
CMD window into the project root directory, enter
PHP Artisan Make:controller Usercontroller
This will create the Usercontroller controller in the app/http/controllers/directory.
Note that if you create a new background controller directory admin/under the app/http/controllers/directory, you need to fix the namespace in the controller file
3. Advanced routing 3.1 named routes
// named Closure routing Array (function() {}, ' as ' = ' Alias '); or Route Array (function() {})->name (' Alias '); // Named Controller routing Array (' uses ' = ' admin\[email protected] ', ' as ' = ' Alias '); or routearray(' uses ' = ' admin\[email protected] '})->name (' Alias ');
3.2 Routing Groupings
3.2.1 Routing prefixes and namespaces
For example, there are two routes that point to a background controller action
Route::get (' Admin/login ', ' admin\[email protected] '); Route:: Get (' Admin/index ', ' admin\[email protected] ');
Obviously, both the URI and the controller action have the same part, so we enable routing groupings to see
// prefix defining a URL prefix, namespace defining a namespace Route::group (arrayfunction() { Route:: Get (' login ', ' [email protected] '); Route:: Get (' index ', ' [email protected] ') ;
Well, much better, the later adjustment will be more convenient.
3.2.2 Resource Routing
1. Create a resource controller Articlecontroller
PHP Artisan Make:controller Articlecontroller
Add a manual method to it Public function article () {}
2. Define Resource Routing
Here I still choose to define under routing groupings
Route::group (arrayfunction() { Route:: Get (' login ', ' [email protected] '); Route:: Get (' index ', ' [email protected] '); Route::resource(' article ', ' Articlecontroller ');});
3. View Routes
PHP artisan route:list//List all current routes
See a lot of resource routing generated:
That's the benefit of resource routing, and defining a lot of it, we just need to add these methods to the list above the Admin\article controller.
4. Testing
When I tested these methods, I used the Chrome browser's postman plugin. A CSRF error occurred while testing the Post method, the workaround reference http://www.cnblogs.com/HD/p/4555369.html
Laravel 5.2, HTTP (advanced) Routing and Controller creation