Laravel中间件 - 返回错误的响应

时间:2016-08-23 17:34:26

标签: laravel-5.2 middleware

我想为暂存网站创建一个非常简单的密码短语入口中间件,但我需要访问错误包(来自网络中间件),但似乎无法弄清楚:

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class AccessProtection 
{

    /**
     * [handle description]
     * @param  [type]  $request [description]
     * @param  Closure $next    [description]
     * @return [type]           [description]
     */
    public function handle( $request, Closure $next )
    {
        if( ! app()->environment( 'production' ) ) {

            /** Checks for a submission for access to the site */
            if( ! is_null( $response = $this->checkForAccessRequest( $request ) ) ) return $response; 
            /** Checks to see if access has already been granted */
            if( ! is_null( $response = $this->checkForAccessPermission() ) ) return $response; 

        }
        return $next( $request );
    }

    /**
     * [checkForAccessPermission description]
     * @return [type] [description]
     */
    protected function checkForAccessPermission()
    {
        if( ! session()->has( 'access_permission' ) ) {
            return response()->make( view( 'global.permission' , [ 'error' => false ]), 401 );
        }
    }

    /**
     * [checkForAccessRequest description]
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    protected function checkForAccessRequest( $request )
    {
        if( $passphrase = $request->input( 'passphrase' ) ) {
            if( strcmp( $passphrase, env( 'PASSPHRASE', 'purpose' ) ) !== 0 ) {
                return response()->make( view( 'global.permission' , [ 'error' => 'Sorry passphrase was incorrect' ]), 401 );
            }
            $request->session()->put( 'access_permission', true );
        }
    }

}

非常感谢任何帮助。

0 个答案:

没有答案