VerifyCsrfToken.php

时间:2016-03-20 22:03:59

标签: php laravel laravel-5.2

  VerifyCsrfToken.php第135行中的

ErrorException

     

尝试获取非对象的属性

导致我出现上述错误的原因是什么?我有一个自定义中间件。

我不确定要发布哪些文件,因为它在我登录时发生,在我的视图中我有

{!! csrf_field() !!}

路线:

<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('welcome');
});

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/

Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'middleware' => 'web'], function() {

    /* Admin Auth */
    Route::get('login', 'Auth\AuthController@getLogin');
    Route::post('login', 'Auth\AuthController@postLogin');
    Route::get('register', 'Auth\AuthController@getRegister');
    Route::post('register', 'Auth\AuthController@postRegister');
    Route::get('logout', 'Auth\AuthController@getLogout');

  Route::group(['middleware' => 'auth.admin'], function(){
    /*Admin Dashboard Routes */
        Route::get('dashboard', 'AdminController@getDashboard');
        Route::get('admin', 'AdminController@getDashboard');    
    });
});



Route::group(['middleware' => 'web'], function () {

    Route::get('/home', 'HomeController@index');
});

中间件:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class AdminAuthController
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if(Auth::guard($guard)->guest())
        {
            if ($request->ajax())
            {
                return response('Unauthorized.', 401);
            }else{
                return redirect()->guest('admin/login');
            }
            return $next($request);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->guest()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        }

        return redirect()->guest('admin/login');
    }
    // they are not a guest, so lets allow the request
    // to continue to the application
    return $next($request);
    // we are returning the response from where ever it started
    // from down the pipeline
}

如果请求符合某些条件,我们会通过不调用$next($request)来阻止它继续深入应用程序。在任何一种情况下,我们最终都会返回一个Response类的某种形式的响应,希望如此。

CSRF中间件正在接收请求,检查它,如果它通过,那么它会将请求更深入地传递到应用程序中。这个调用返回一个它想要添加cookie的响应,但为了做到这一点,它期待一个特定类型的对象。

要很好地参与此堆栈,您必须要求进入并返回响应。

  

要将请求更深入地传递到应用程序中(允许中间件“通过”),只需使用$ request调用$ next回调。

     

最好将中间件视为一系列“层”,HTTP请求必须在它们到达您的应用程序之前通过。每个层都可以检查请求,甚至完全拒绝它。

     

Laravel Docs - Middleware - Defining Middleware