博客列表 >创建一个路由中间件,并通过访问url地址来触发中间件输出一名话:‘hello middware’-2019-11-08

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

H先生
H先生原创
2019年11月11日 19:56:151009浏览

实例



1.png

<?php
namespace App\Http\Middleware;

use Closure;

class CheckAge
{


    public function handle($req,Closure $next){
        $username = 'admin';
        if ($username!='admin'){
            exit ('不旭旭看');
        }

//        echo 'checkAge中间件执行完毕';
      return  $next($req);
    }

}
?>

运行实例 »

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

实例

use App\Models\Director;

use Illuminate\Http\Request;

class Home extends Controller
{
    /*public function index(Director $users){
        echo '<pre>';
        $res = $users->cc();
        foreach($res as $k => $val){
            $res[$k] = (array)$val;
        }
        $users = Director::find(1);
        print_r($users->name);
    }*/

    /*public function index(Request $req){
        $id = $_GET['director'];
        $id = $req->director;
    }*/
    public function index(){
        echo 'hello middware';
    }
}

运行实例 »

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

实例

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        // 自定义的中间件
        'checkage' => \App\Http\Middleware\CheckAge::class,
    ];

    /**
     * The priority-sorted list of middleware.
     *
     * This forces non-global middleware to always be in the given order.
     *
     * @var array
     */
    protected $middlewarePriority = [
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\Authenticate::class,
        \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \Illuminate\Auth\Middleware\Authorize::class,
    ];
}

运行实例 »

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

实例

<?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!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/304525', function () {
    // 读取数据
    $article = array('title'=>'2019国庆大阅兵','detail'=>'奖励上级阿萨德老费劲啊胜利大街爱上了就发了房间里睡大觉','auth'=>array('username'=>'admin','age'=>18));
    return view('article/detail',$article);
});

Route::get('/home','home@index')->middleware('checkage');

// 1.解析、home
// 2.$home = new Home();

// 3.调用$home->index();




Route::get('/article/detail',function (){
    return ('<div style="margin: 10px auto;text-align:center; font-weight: bold;color: red;">新闻列表</div>');
});

Route::get('/admin/home/index','admin\Home@index');

Route::get('/admin/home/lists','admin\Home@lists');

// 登录
Route::get('/admin/home/login','admin\Home@login');
Route::post('/admin/home/dologin','admin\Home@dologin');


Route::get('/dbtest/index','Dbtest@index');

Route::get('/director/index','Director@index');

运行实例 »

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


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议