简体   繁体   中英

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

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. 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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