简体   繁体   English

自定义Laravel 5.1中间件逻辑

[英]Custom Laravel 5.1 Middleware Logic

I'm stuck in this issue where I'm facing problem setting up my custom middleware in Laravel 5.1 我陷入了这个问题,在Laravel 5.1中设置自定义中间件时遇到了问题

Here's what I want. 这就是我想要的。

I want user to go through setup steps when He logs in to my app. 我希望用户在他登录到我的应用程序时经历设置步骤。 If he completes the setup he should not see that setup page again. 如果他完成了设置,他将不会再看到该设置页面。 Also I'm setting this middleware in global array. 我也在全局数组中设置此中间件。

here's my middleware. 这是我的中间件。

<?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);
    }
}

Also he shouldn't see the step which is already completed. 而且他不应该看到已经完成的步骤。 Suppose if user has created the related model "school" he should be redirected to 'setup/payement' url instead of 'setup/info'. 假设用户创建了相关模型“学校”,则应将其重定向到“设置/付款”网址,而不是“设置/信息”网址。 right now I can login and visit every step even when I have completed all the steps. 现在即使完成所有步骤,我也可以登录并访问每个步骤。

Manually redirecting to each step if the condition matches is the only option I see so far but that approach makes my controller too muddy. 到目前为止,我看到的唯一选择是如果条件匹配,则手动重定向到每个步骤,但是这种方法会使我的控制器过于混乱。

It doesn't seem like you understand the programming flow. 您似乎不了解编程流程。 If you do $this->auth->user()->active == false theres no need to do $this->auth->user()->active != false as if it would have false it would have returned a redirect header. 如果您执行$this->auth->user()->active == false无需执行$this->auth->user()->active != false ,就好像它具有false一样,它会返回重定向标头。 Same thing for all your other conditions. 其他所有条件也一样。

Now to handle the actual issue ,instead of using a middleware you should use form-request approach and then by session save the user current step and redirect him by the data you get from session.[should create sort of a helper to handle the actual routes redirecting] 现在要处理实际问题,而不是使用middleware您应该使用表单请求方法,然后通过会话保存用户当前步骤,并通过从会话中获取的数据来重定向用户。[应该创建一个帮助程序来处理实际问题路由重定向]

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

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