An example of HTTP routing and creation controller and resource routing in Laravel5.2

Source: Internet
Author: User

One, HTTP routing

All routes are defined in the app/http/routes.php file that is loaded into the App\providers\routeserviceprovider class.

1. Basic Routing

A simple Laravel route accepts only one URI and one closure


Route::get (' foo ', function () {    return ' Hello, laravel! ';});

For common HTTP requests, Laravel has the following types of routes


Route::get ($uri, $callback); Response GET request 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 '], $uri, $callback); Response get, POST request Route::any (' foo ', $callback); Respond to all requests

Where $callback can be a closure or a controller method. In fact, there are many situations in development that are used as controller methods.

2. Routing parameters


Single route parameter Route::get (' User/{id} ', function ($id) {    return ' user '. $id;}); /Multiple route parameters Route::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 Can only be set to the last parameter optional, other location settings optional Parse error//Regular constraint single parameter Route::get (' User/{name?} ', function ($name = ' Jone ') {    return $name;}) ->where (' name ', ' \w+ ');  Constraint parameters are word characters (numbers, letters, underscores)//Regular constraints multiple parameters Route::get (' user/{id}/{name} ', function ($id, $name) {    //})->where ([' id ' = > ' [0-9]+ ', ' name ' = ' [a-z]+ '];

Second, create the controller

Use the Artisan command to create a php Artisan make:controller usercontroller

Now, the controller file for Usercontroller. PHP is generated under the App/http/controllers controller directory.

Third, advanced routing

1. Named routes


Named closure Route route:get (' user ', Array (' as ' = ' alial ', function () {});//or Name method chain Route:get (' User ', function () {}) Name (' Alias ');//Name Controller method route Route:get (' user ', Array (' uses ' = ' = ' admin\indexcontroller@index ', ' as ' = ' Alias ')); or the name method chain Route:get (' user ', ' admin\indexcontroller@index ')->name (' Alias '));

2. Routing groupings

2.1 Route prefixes and namespaces

For example, there are two routes that point to the Controller method


Route::get (' Admin/login ', ' admin\indexcontroller@login '); Route::get (' Admin/index ', ' admin\indexcontroller@index ');

Take the first one,

Parameter one:admin/login represents this URI in the request site root directory of Admin/login resources, the full address is http:// Domain name/admin/login (This opens the Apache's route rewrite, which hides "index.php"), this request is mapped to the controller method specified in the second parameter. Note that the site root directory is the directory where the portal files are located, the public directory is in Laravel, and it is best to configure the server to also point here.

Parameter two:admin\indexcontroller@login means that the Controller method is under the app\http\controllers namespace, connected to App\http\ The login method in the Controllers\admin\indexcontroller controller.

Obviously, both routes have the same URI and controller methods, so enabling routing groupings can extract the public part:


In the first array parameter, the prefix key defines the public part of the URI, and the namespace key defines the public part of the method name (namespace syntax) Route::group (Array (' prefix ' = ' admin ', ' namespace ' =& Gt ' Admin '), function () {    route::get (' login ', ' indexcontroller@login ');    Route::get (' index ', ' Indexcontroller@index ');});

2.2 Resource Routing

Resource routing is the route mapped to the resource controller, and the Laravel resource controller has built up 7 methods and 7 routes for adding and deleting resources.

First, create the resource controller Articlecontroller


PHP artisan make:controller admin\articlecontroller  --resource

Please ensure that the Admin directory exists under the App/http/controllers directory for the above command.

This generates the resource controller in the app/http/controllers/admin/articlecontroller.php file, with the built-in 7 methods as follows:


<?phpnamespace app\http\controllers\admin;use illuminate\http\request;use app\http\requests;use App\Http\ Controllers\controller;class Linkscontroller extends controller{/** * Displays a list of resources * * @return \ILLUMINATE\HT Tp\response */Public Function index () {//}/** * Show a table forms create a new resource * * @return \illumi Nate\http\response * * Public Function Create () {//}/** * Save newly created resource * * @param \illu    Minate\http\request $request * @return \illuminate\http\response * * Public function Store (Request $request) {//}/** * Displays the specified resource * * @param int $id * @return \illuminate\http\response */Publ IC function Show ($id) {//}/** * Displays a form to edit the specified resource * * @param int $id * @return \illumina Te\http\response */Public Function edit ($id) {//}/** * Update specified resource * * @param \illumin Ate\http\request $request * @param int $id * @return \illuminate\http\response */Public Function update (Request $request, $id) {// }/** * Deletes the specified resource * * @param int $id * @return \illuminate\http\response */Public function Destroy ($ID) {//}}

Then, define the resource route . Here I still choose to define under routing groupings, define one.


Route::group (' prefix ' = ' admin ', ' namespace ' = ' admin '), function () {     route::get (' Login ', ' Indexcontroller@login ');    Route::get (' index ', ' Indexcontroller@index ');    Resource Routing    route::resource (' article ', ' Articlecontroller ');});

Finally, look at the route. With resource controller and resource routing, you can look at the HTTP request method for the above 7 methods.

Use the Artisan command PHP Artisan route:list to list all current routes, the request method, URI, controller method, middleware are listed.

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.