This article brings you an introduction to HTTP routing, creation of controllers and resource routing in Laravel 5.2 (with code). It has certain reference value. Friends in need can refer to it. I hope it will help You helped.
1. HTTP routing
All routes are defined in the app/Http/routes.php file loaded by the App\Providers\RouteServiceProvider class.
Simple Laravel routing only accepts a URI and a closure
Route::get('foo', function () { return 'Hello, Laravel!'; });
For common For HTTP requests, Laravel has the following routes
Route::get($uri, $callback); //响应 get 请求 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); //响应 get, post 请求 Route::any('foo', $callback); //响应所有请求
Among them, $callback can be a closure or a controller method. In fact, there are many situations in development where controller methods are used.
//单个路由参数 Route::get('user/{id}', function ($id) { return 'User '.$id; }); //多个路由参数 Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) { // }); //单个路由参数(可选) Route::get('user/{id?}', function ($id = 1) { return 'User '.$id; }); //多个路由参数(可选) Route::get('posts/{post}/comments/{comment?}', function ($postId, $commentId = 1) { // }); //注意:多个参数时,只可以对最后一个参数设置可选,其他位置设置可选会解析错误 // 正则约束单个参数 Route::get('user/{name?}', function ($name = 'Jone') { return $name; })->where('name', '\w+'); //约束参数为单词字符(数字、字母、下划线) // 正则约束多个参数 Route::get('user/{id}/{name}', function ($id, $name) { // })->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
2. Create controller
Use Artisan command to create php artisan make:controller UserController
Now, the controller file UserController.php is generated in the controller directory app/Http/Controllers.
3. Advanced routing
1. Named route
//命名闭包路由 Route:get('user', array('as' => 'alial', function(){}); //或 name 方法链 Route:get('user', function(){})->name('alias'); //命名控制器方法路由 Route:get('user', array('uses' => 'Admin\IndexController@index', 'as' => 'alias')); //或 name 方法链 Route:get('user', 'Admin\IndexController@index')->name('alias'));
2. Routing group
2.1 Routing prefix sum Namespace
For example, there are two routes pointing 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 means that this URI is requesting the admin/login resource in the root directory of the website. The complete address is http://domain name/admin/login (Apache's route rewriting is turned on here, hiding "index.php") , this request is mapped to the controller method specified in the second parameter. Note that the website root directory is the directory where the entry file is located, which is the public directory in Laravel. It is best to point here when configuring the server.
Parameter two: Admin\IndexController@login means that this controller method is in the App\Http\Controllers namespace, and the connection is App\Http\Controllers\ The login method in the Admin\IndexController controller.
Obviously, the URI and controller method of the two routes have the same parts. Then, enabling route grouping can extract the common part:
// 第一个数组参数中,prefix 键定义 URI 的公共部分,namespace 键定义方法名(命名空间语法)的公共部分 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 route mapped to the resource controller. Laravel resource controller has built-in 7 methods for adding, deleting, modifying and checking resources and 7 routes.
First, create the resource controller ArticleController
php artisan make:controller Admin/ArticleController --resource
This will generate the resource controller in the app/Http/Controllers/Admin/ArticleController.php file (Admin folder It will be created automatically if it does not exist). The 7 built-in methods are as follows:
<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; class LinksController extends Controller { /** * 显示一个资源的列表 * * @return \Illuminate\Http\Response */ public function index() { // } /** * 显示一个表单来创建一个新的资源 * * @return \Illuminate\Http\Response */ public function create() { // } /** * 保存最新创建的资源 * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // } /** * 显示指定的资源 * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * 显示一个表单来编辑指定的资源 * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // } /** * 更新指定的资源 * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // } /** * 删除指定的资源 * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // } }
Then, define resource routing. Here I still choose to define it under the routing group, just define one
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin'), function(){ Route::get('login', 'IndexController@login'); Route::get('index', 'IndexController@index'); // 资源路由 Route::resource('article', 'ArticleController'); });
Finally, check the route. With resource controller and resource routing, you can look at the HTTP request methods for the above 7 methods.
Use the Artisan command php artisan route:list to list all current routes, including request methods, URIs, controller methods, and middleware.
The above is the detailed content of Introduction to HTTP routing, creating controllers and resource routing in Laravel5.2 (with code). For more information, please follow other related articles on the PHP Chinese website!