登录  /  注册
首页 > php框架 > Laravel > 正文

laravel中间件都有哪些

青灯夜游
发布: 2021-09-14 17:41:40
原创
2881人浏览过

中间件有:1、authenticate;2、checkformaintenancemode;3、encryptcookies;4、redirectifauthenticated;5、trimstrings;6、trustproxies等等。

laravel中间件都有哪些

本教程操作环境:windows7系统、Laravel6版、Dell G3电脑。

Laravel自带的中间件

Laravel 自带了一些中间件,包括身份验证、CSRF 保护等。Laravel 具体启用了哪些中间件,可通过 app\Http\Kernel.php 文件查看。对于以 \App\Http\Middleware\ 开头的中间件(位于 app/Http/Middleware 目录)是我们可以对其行为进行定制的中间件。

Authenticate 中间件

源文件:app\Http\Middleware\Http\Middleware\Authenticate.php

<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
class Authenticate extends Middleware
{
    /**
     * Get the path the user should be redirected to when they are not authenticated.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return string
     */
    protected function redirectTo($request)
    {
        if (! $request->expectsJson()) {
            return route(&#39;login&#39;);
        }
    }
}
登录后复制

作用:

用户身份验证。可修改 redirectTo 方法,返回未经身份验证的用户应该重定向到的路径。

CheckForMaintenanceMode 中间件

源文件 :app\Http\Middleware\CheckForMaintenanceMode.php

<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;
class CheckForMaintenanceMode extends Middleware
{
    /**
     * The URIs that should be reachable while maintenance mode is enabled.
     *
     * @var array
     */
    protected $except = [
        //
    ];
}
登录后复制

作用:

检测项目是否处于 维护模式。可通过 $except 数组属性设置在维护模式下仍能访问的网址。

EncryptCookies 中间件

源文件:app\Http\Middleware\EncryptCookies.php

<?php
namespace App\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
class EncryptCookies extends Middleware
{
    /**
     * The names of the cookies that should not be encrypted.
     *
     * @var array
     */
    protected $except = [
        //
    ];
}
登录后复制

作用

对 Cookie 进行加解密处理与验证。可通过 $except 数组属性设置不做加密处理的 cookie。

RedirectIfAuthenticated 中间件

源文件:app\Http\Middleware\RedirectIfAuthenticated.php

<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect(&#39;/home&#39;);
        }
        return $next($request);
    }
}
登录后复制

作用:

当请求页是 注册、登录、忘记密码 时,检测用户是否已经登录,如果已经登录,那么就重定向到首页,如果没有就打开相应界面。可以在 handle 方法中定制重定向到的路径。

TrimStrings 中间件

源文件:app\Http\Middleware\TrimStrings.php

<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;
class TrimStrings extends Middleware
{
    /**
     * The names of the attributes that should not be trimmed.
     *
     * @var array
     */
    protected $except = [
        &#39;password&#39;,
        &#39;password_confirmation&#39;,
    ];
}
登录后复制

作用:

对请求参数内容进行 前后空白字符清理。可通过 $except 数组属性设置不做处理的参数。

TrustProxies 中间件

源文件:app\Http\Middleware\TrustProxies.php

<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Fideloper\Proxy\TrustProxies as Middleware;
class TrustProxies extends Middleware
{
    /**
     * The trusted proxies for this application.
     *
     * @var array|string
     */
    protected $proxies;
    /**
     * The headers that should be used to detect proxies.
     *
     * @var int
     */
    protected $headers = Request::HEADER_X_FORWARDED_ALL;
}
登录后复制

作用:

配置可信代理。可通过 $proxies 属性设置可信代理列表,$headers 属性设置用来检测代理的 HTTP 头字段。

VerifyCsrfToken 中间件

源文件:app\Http\Middleware\VerifyCsrfToken.php

<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
    /**
     * Indicates whether the XSRF-TOKEN cookie should be set on the response.
     *
     * @var bool
     */
    protected $addHttpCookie = true;
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
    ];
}
登录后复制

作用:

验证请求里的令牌是否与存储在会话中令牌匹配。可通过 $except 数组属性设置不做 CSRF 验证的网址。

相关推荐:最新的五个Laravel视频教程

以上就是laravel中间件都有哪些的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号