Blogger Information
Blog 22
fans 0
comment 0
visits 18353
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
lavarel中间件创建和执行---2019-11-08
sjgbctjda的博客
Original
789 people have browsed it

1、创建一个路由中间件,并通过访问url地址来触发中间件输出一名话:‘hello middware’

中间件代码:

<?php

namespace App\Http\Middleware;

use Closure ;


class myMiddleware 
{
    public function handle($req,Closure  $next){
        echo 'hello middleware';
        return $next($req);
    }
}

控制器代码:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Movies;

class Home extends Controller
{
    public function getInfo(Movies $movies){
        echo '<pre>';
        // $res = $staff->select();
        $res = $movies->select();
        print_r($res);

    }
    public function index(){
        // echo '<hr>';
        echo '中间件已运行';
    }
}

路由:

Route::get('home','home@index')->middleware('myMiddleware');
运行结果:

image.png


小结:

    路由中间件创建的是类中的函数名称是固定为handle,它的第二个参数为闭包类型;路由中间件创建完成后需要先注册,然后在路由中使用middleware()方法调用,参数为注册时中间的键值。

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