Detailed explanation of examples of HTTP routing and creation of controllers and resource routing in Laravel5.2

黄舟
Release: 2023-03-15 21:52:01
Original
1894 people have browsed it

1. HTTP routing

All routes are defined in the app/Http/routes.php file loaded by the App\Providers\RouteServiceProvider class.

1. Basic routing

Simple Laravel routing only accepts a URI and a closure


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

For common 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); //响应所有请求
Copy after login

Among them, $callback can be a closure or a controller method. In fact, there are many situations in development where controller methods are used.

2. Routing parameters


//单个路由参数
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]+']);
Copy after login

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 routing


//命名闭包路由
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'));
Copy after login

2. Routing group

2.1 Route prefixes and namespaces

For example, there are two routes pointing to controller methods


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

Take the first For example,

Parameter 1: admin/login means that this URI is requesting the admin/login resource under the website root directory, and the full address is http://domain name/admin/login (Apache's route rewriting is enabled 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\Admin\IndexController The login method in the controller.

Obviously, the URIs and controller methods of the two routes have the same parts. Then, enabling route grouping can extract the common parts:


// 第一个数组参数中,prefix 键定义 URI 的公共部分,namespace 键定义方法名(命名空间语法)的公共部分
Route::group(array('prefix' => 'admin', 'namespace' => 'Admin'), function(){
    Route::get('login', 'IndexController@login');
    Route::get('index', 'IndexController@index');
});
Copy after login

2.2 Resource routing

Resource routing is the route mapped to the resource controller. Laravel resource controller has built-in 7 ways to add, delete, modify and check resources and 7 routes.

First, create Resource Controller ArticleController


php artisan make:controller Admin\ArticleController  --resource
Copy after login

Please ensure that the Admin directory exists in the app/Http/Controllers directory for the above command .

This generates the resource controller file in app/Http/Controllers/Admin/ArticleController.php. 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)
    {
        //
    }
}
Copy after login

Then, define resource routing . Here I still choose to define it under the routing group, just define one


Route::group(array(&#39;prefix&#39; => &#39;admin&#39;, &#39;namespace&#39; => &#39;Admin&#39;), function(){ 
    Route::get(&#39;login&#39;, &#39;IndexController@login&#39;);
    Route::get(&#39;index&#39;, &#39;IndexController@index&#39;);
    // 资源路由
    Route::resource(&#39;article&#39;, &#39;ArticleController&#39;);
});
Copy after login

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 Detailed explanation of examples of HTTP routing and creation of controllers and resource routing in Laravel5.2. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!