繁体   English   中英

自定义Laravel 5.1中间件逻辑

[英]Custom Laravel 5.1 Middleware Logic

我陷入了这个问题,在Laravel 5.1中设置自定义中间件时遇到了问题

这就是我想要的。

我希望用户在他登录到我的应用程序时经历设置步骤。 如果他完成了设置,他将不会再看到该设置页面。 我也在全局数组中设置此中间件。

这是我的中间件。

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\RedirectResponse;

class SetupMiddleware
{


    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }


    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {

        if($this->auth->check()){


            if( $this->auth->user()->active == false ){

                if ($request->ajax())
                {
                    return response('Not verified.', 401);
                }
                else
                {
                    return redirect('/user/verification');
                }

            }



            if( $this->auth->user()->active != false &&  null === Auth::user()->school ){

                if ($request->ajax())
                {
                    return response('Not allowed.', 401);
                }
                else
                {
                    return redirect('/setup/info');
                }

            }



            if( $this->auth->user()->active != false &&  null !== Auth::user()->school &&  $this->auth->user()->payment_active == false ){

                if ($request->ajax())
                {
                    return response('Not allowed.', 401);
                }
                else
                {
                    return redirect('/setup/payment');
                }

            }



            if( $this->auth->user()->active != false && null !== Auth::user()->school &&  $this->auth->user()->payment_active != false ){


                return $next($request);

            }

            return $next($request);
        }


        return $next($request);
    }
}

而且他不应该看到已经完成的步骤。 假设用户创建了相关模型“学校”,则应将其重定向到“设置/付款”网址,而不是“设置/信息”网址。 现在即使完成所有步骤,我也可以登录并访问每个步骤。

到目前为止,我看到的唯一选择是如果条件匹配,则手动重定向到每个步骤,但是这种方法会使我的控制器过于混乱。

您似乎不了解编程流程。 如果您执行$this->auth->user()->active == false无需执行$this->auth->user()->active != false ,就好像它具有false一样,它会返回重定向标头。 其他所有条件也一样。

现在要处理实际问题,而不是使用middleware您应该使用表单请求方法,然后通过会话保存用户当前步骤,并通过从会话中获取的数据来重定向用户。[应该创建一个帮助程序来处理实际问题路由重定向]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM