zoukankan      html  css  js  c++  java
  • laravel的中间件

    laravel中间件的使用

    laravel内置了一个中间件来验证用户是否经过认证,如果用户没有经过认证,中间件会将用户重定向到登录页面,否则如果用户经过认证,中间件就会允许请求继续往前进入下一步操作。

    当然,除了认证之外,中间件还可以被用来处理更多其它任务。比如:CORS 中间件可以用于为离开站点的响应添加合适的头(跨域);日志中间件可以记录所有进入站点的请求。

    Laravel框架自带了一些中间件,包括认证、CSRF 保护中间件等等。所有的中间件都位于 app/Http/Middleware目录。

    <?php
    
    namespace AppHttpMiddleware;
    
    use Closure;
    
    class TestMiddle
    {
    
        public function handle($request, Closure $next)
        {
            // 执行动作
         
            if(!$request->session()->has('huser')){
                return redirect("login/index");
            }
            return $next($request);
            
            
        }
    }
    

      

    而下面这个中间件则会在请求处理后执行其任务:

     
    <?php
    
    namespace AppHttpMiddleware;
    
    use Closure;
    
    class TestMiddle
    {
    
        public function handle($request, Closure $next)
        {
            $response = $next($request);
    
            // 执行动作
            if(!$request->session()->has('huser')){
                return redirect("login/index");
            }
            
            return $response;
        
            
            
        }
    }
  • 相关阅读:
    C he 指针
    typedef 与 define
    (转)ubuntu中安装man手册查看函数原型
    .9 赫夫曼编码
    .8 AVL树
    PowerDesigner使用技巧
    C#基础
    NET框架设计
    Sql Server 执行计划及Sql查询优化
    SQL SERVER函数浅析
  • 原文地址:https://www.cnblogs.com/LQK157/p/11491246.html
Copyright © 2011-2022 走看看