Blogger Information
Blog 31
fans 1
comment 5
visits 29594
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
创建一个路由中间件-2019-11月8号作业
零度 的博客
Original
890 people have browsed it
  1. 用命令行创建一个index名控制器文件 php artisan make:controller index


    实例

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    class index extends Controller
    {
    public  function index(){
    
    return 'index执行';
    }
    
    }

    运行实例 »

    点击 "运行实例" 按钮查看在线实例

2.用命令行创建一个check名中间件文件php artisan make:middleware check

实例

<?php

namespace App\Http\Middleware;

use Closure;

class check
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
//        输出中间件的内容
        echo 'hello middware <hr>';

        return $next($request);
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

3.打开I:\laravel58\app\Http\Kernel.php这个文件配置注册路由中间件,直接上图吧

1.jpg

4.路由代码

实例

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

//给index路由加个中间件
//用法:路由后面写->middleware('中间件名称')
Route::get('/index','index@index')->middleware('check');

运行实例 »

点击 "运行实例" 按钮查看在线实例

5.结果图:

2.jpg

总结:

这是最简单的方式,感觉不难,用命令创建两分钟就搞定了,作业没要求别的就这样写了,这作业真的不难!

Correction status:qualified

Teacher's comments:中间作作用很大, 不要小看
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post