VerifyCsrfToken.php 中的错误异常
ErrorException in VerifyCsrfToken.php
ErrorException
in VerifyCsrfToken.php line 135:
Trying to get property of non-object
是什么导致我出现上述错误?我有一个自定义中间件。
我不确定要 post 哪些文件,因为它发生在我登录时,在我看来我有
{!! 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);
}
}
}
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)
来阻止它继续深入应用程序。在任何一种情况下,我们最终都会 returning 一个 Response
class 的某种形式的响应,希望如此。
CSRF 中间件正在接受一个请求,对其进行检查,如果通过,则将请求更深入地传递到应用程序中。此调用 return 是一个响应,它也想添加一个 cookie,但为此它需要特定类型的对象。
要很好地参与此堆栈,您必须期待收到请求并 return 返回响应。
To pass the request deeper into the application (allowing the middleware to "pass"), simply call the $next callback with the $request.
It's best to envision middleware as a series of "layers" HTTP requests must pass through before they hit your application. Each layer can examine the request and even reject it entirely.
ErrorException
in VerifyCsrfToken.php line 135:Trying to get property of non-object
是什么导致我出现上述错误?我有一个自定义中间件。
我不确定要 post 哪些文件,因为它发生在我登录时,在我看来我有
{!! 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);
}
}
}
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)
来阻止它继续深入应用程序。在任何一种情况下,我们最终都会 returning 一个 Response
class 的某种形式的响应,希望如此。
CSRF 中间件正在接受一个请求,对其进行检查,如果通过,则将请求更深入地传递到应用程序中。此调用 return 是一个响应,它也想添加一个 cookie,但为此它需要特定类型的对象。
要很好地参与此堆栈,您必须期待收到请求并 return 返回响应。
To pass the request deeper into the application (allowing the middleware to "pass"), simply call the $next callback with the $request.
It's best to envision middleware as a series of "layers" HTTP requests must pass through before they hit your application. Each layer can examine the request and even reject it entirely.