簡體   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