Introduction of HTTP Routing, Creating Controller and Resource Routing in Laravel5.2 (with code)

01-28-2024

This article is about the introduction of HTTP routing, creating controller and resource routing in Laravel5.2 (with code attached), which has certain reference value. Friends who need it can refer to it and hope it will help you. I. HTTP routing

All routes are defined in the app/Http/routes.php file loaded by the app \ providers \ routeserviceprovider class.

1. Laravel routing with simple basic routing only accepts a URI and a closure.

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

Laravel has the following routes for common HTTP requests.

Route::get($uri, $callback); //respond to the get request Route::post($uri, $callback); Route::put($uri, $callback); Route::patch($uri, $callback); Route::delete($uri, $callback); Route::options($uri, $callback); Route::match(['get', 'post'], $uri, $callback); //respond to 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 cases in development that are used as controller methods.   

2. Routing parameters//Single routing parameters Route::get('user/{id}', function ($id) { return 'User '.$id; }); //Multiple routing parameters Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { // }); //Single routing parameter (optional) Route::get('user/{id? }', function ($id = 1) { return 'User '.$id; }); //Multiple routing parameters (optional) Route::get('posts/{post}/comments/{comment? }', function ($postId, $commentId = 1) { // }); //Note: When there are multiple parameters, only the last parameter can be set to be optional, and setting other positions to be optional will resolve errors. //Regularly constrain a single parameter Route::get('user/{name? }', function ($name = 'Jone') { return $name; })->where('name', '\w+'); //Constraint parameters are word characters (numbers, letters, underscores) //Regularly constrain multiple parameters Route::get('user/{id}/{name}', function ($id, $name) { // })->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

Second, create a controller

Use the Artisan command to create PHP artisan make: controller user controller.

Now, the controller file of UserController.php is generated in the controller directory of app/Http/Controllers.

Third, advanced routing

1. Named route

//Named closure route Route:get('user', array('as' => 'alial', function(){}); //or name method chain Route:get('user', function(){})->name('alias'); //Named controller method route Route:get('user', array('uses' => 'Admin\IndexController@index', 'as' => 'alias')); //or name method chain Route:get('user', 'Admin\IndexController@index')->name('alias'));

2. Routing packets

2.1 Routing Prefix and Namespace

For example, there are two routes to the controller method.

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

Take the first one,

Parameter 1: admin/login indicates the admin/login resource of this URI under the root directory of the request website, and the full address is http:// domain name /admin/login (where Apache's route rewriting is turned on and index.php is hidden), and this request is mapped to the controller method specified in the second parameter. Note that the root directory of the website is the directory where the entry file is located, and it is the public directory in Laravel, so it is best to point here when configuring the server.

Parameter 2: Admin\IndexController@login indicates that this controller method is in the App\Http\Controllers namespace, and together it is the login method in the app \ http \ controllers \ admin \ indexcontroller controller.

Obviously, the URI and controller method of both routes have the same part, so enabling routing grouping can extract the common 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' => 'Admin'), function(){ Route::get('login', 'IndexController@login'); Route::get('index', 'IndexController@index'); });

2.2 Resource Routing

Resource routing is the routing mapped to the resource controller, and Laravel resource controller has seven methods and seven routes built in.

First, create the resource controller ArticleController.

php artisan make:controller Admin/ArticleController --resource

In this way, the resource controller file in app/http/controllers/Admin/articlecontroller.php (which will be automatically created when the admin folder does not exist) is generated, and the built-in seven methods are as follows:

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us