laravel 中间件基本使用

用户请求最常见使用的中间件、或者请求参数的处理和用户身份的校验

前置

aop 就是把相同的操作单独提取某一个地方,这个地方可能是中间件,也可能是一个事件,只写一次

一、创建中间件

E:\xampp\htdocs\xampp\blog>php artisan make:middleware OldMiddleware
<?php
namespace App\Http\Middleware;
use Closure;
use Response;

class OldMiddleware
{
    public function handle($request, Closure $next)
    {
        echo 'hello 中间件 <br>';

         #保安
         #假设用户没有一个token 就不允许通过
        if (!$request->input('token')) {
            return Response::make('没有门票不能通过');
        }
         #aop 面向于切面 ==》 面向过程补充

        return $next($request); // $next($request)执行用户的请求操作
         // $response;
    }
}

路径
E:\xampp\htdocs\xampp\blog\app\Http\Middleware\OldMiddleware.php

二、先注册路由中间件

E:\xampp\htdocs\xampp\blog\app\Http\Kernel.php 
    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,
        'old'           => \App\Http\Middleware\OldMiddleware::class,
    ];

路由

use App\Contracts\DBContracts;
#路由中间件
Route::get('demo', function(DBContracts $db){
    return $db->index();
})->middleware('old');

#契约

<?php
namespace App\Contracts;

/**
 *
 */
interface DBContracts
{
    public function index();
}

#注册提供服务者

#路径 E:\xampp\htdocs\xampp\blog\app\Providers\RinkServicePrivoder.php

    public function register()
    {
        #向我们容器中注入信息           #这就是标识   这就是我们标识所对应的类
        $this->app->singleton('dbfacade', \App\Utils\MySql::class);
        // $this->app->singleton('mysql', \App\Services\MysqlService::class);

        // 契约和实例类做一个绑定       契约的类                          实例类
        $this->app->singleton(\App\Contracts\DBContracts::class, \App\Utils\Orcale::class);
        // \App\Utils\MySQL::class =》  \App\Utils\Oracle::class
    }

路径 public\index.php

#传进去契约,解析出来实例
#Illuminate\Contracts\Http\Kernel::class 解析的是app/Http/kernel.php这个文件
#获取容器使用make,用这个字符串获取出他的对象
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle( //执行用户的请求
    $request = Illuminate\Http\Request::capture()       #获取用户请求参数
);

$response->send();  //输出结果

$kernel->terminate($request, $response); //停止用户所开启的中间件

正常的
在这里插入图片描述

不正常的
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伟伟哦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值