如何在 Laravel 中解析 "ErrorException in VerifyCsrfToken.php"
How to resolve "ErrorException in VerifyCsrfToken.php" in Laravel
这是我的方法:
我 运行 以下 PHP Artisan 命令:
php artisan make:middleware AfterCacheMiddleware
php artisan make:middleware BeforeCacheMiddleware
然后,我更新了我新创建的中间件(AfterCacheMiddleware
和 BeforeCacheMiddleware
),如下所示:
AfterCacheMiddleware
class AfterCacheMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
$key = $this->keygen($request->url());
if( ! Cache::has( $key ) ) Cache::put( $key, $response->getContent(), 1 );
return $response;
}
protected function keygen( $url )
{
return 'route_' . Str::slug( $url );
}
}
BeforeCacheMiddleware
namespace App\Http\Middleware;
use Closure;
use Cache;
use Str;
class BeforeCacheMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$key = $this->keygen($request->url());
if( Cache::has( $key ) ) return Cache::get( $key );
return $next($request);
}
protected function keygen( $url )
{
return 'route_' . Str::slug( $url );
}
}
此外,我已经在 app/Http/Kernel.php
中注册了两个中间件
'App\Http\Middleware\AfterCacheMiddleware',
'App\Http\Middleware\BeforeCacheMiddleware',
'cacheafter' => 'App\Http\Middleware\AfterCacheMiddleware',
'cachebefore' => 'App\Http\Middleware\BeforeCacheMiddleware',
下面是我的路线:
Route::get('middle', ['middleware' => 'cachebefore','cacheafter', function()
{
echo "success";
}]);
在第一个页面加载时,一切正常(因为页面没有被缓存)。
在随后的访问中,我收到此错误:ErrorException in VerifyCsrfToken.php line 76: Trying to get property of non-object
.
我认为错误源于这一行:
if( Cache::has( $key ) ) return Cache::get( $key );
但是我想不通
试试吧。
<?php namespace App\Http\Middleware;
use Closure;
use Cache;
use Illuminate\Support\Str;
use Event;
class AfterCacheMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
/* Event::listen('illuminate.query', function($query)
{
var_dump($query);
return false;
});*/
$response = $next($request);
$key = $this->keygen($request->url());
$key2 = $this->keygen($request->url()).'_2';
if( ! Cache::has( $key ) )
{
Cache::put( $key, $response->getContent(), 1 );
Cache::put( $key2, $response->headers, 1);
}
return $next($request);
}
protected function keygen( $url )
{
return 'route_' . Str::slug( $url );
}
}
<?php namespace App\Http\Middleware;
use Closure;
use Cache;
use Illuminate\Support\Str;
class BeforeCacheMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$key = $this->keygen($request->url());
$key2 = $this->keygen($request->url()).'_2';
if( Cache::has( $key )&& Cache::has( $key2 ) )
{
$content=Cache::get( $key );
$header=Cache::get( $key2 );
$header=explode('Cache-Control:',$header);
$header1=explode('Content-Type:',$header[1]);
$header2=explode('Date:',$header1[1]);
$header3=explode('Set-Cookie:',$header2[1]);
$CacheControl=trim($header1[0]);
$ContentType=trim($header2[0]);
$Date=trim($header3[0]);
$SetCookie=trim($header3[1]);
return response($content)
->header('Cache-Control',$CacheControl)
->header('Content-Type',$ContentType)
->header('Date',$Date)
->header('Set-Cookie',$SetCookie);
}
return $next($request);
}
protected function keygen( $url )
{
return 'route_' . Str::slug( $url );
}
}
不需要添加所有这些东西。如果你也想更新 header 信息,这些很好。
在你之前的cacheiddleware中:
if( Cache::has( $key ) ) return Cache::get( $key );
按照@Den 的建议修改上面的代码
if( Cache::has( $key ) )
{
$content=Cache::get( $key );
return response($content);
}
这会奏效。如果您有任何问题,请返回。
这是我的方法:
我 运行 以下 PHP Artisan 命令:
php artisan make:middleware AfterCacheMiddleware
php artisan make:middleware BeforeCacheMiddleware
然后,我更新了我新创建的中间件(AfterCacheMiddleware
和 BeforeCacheMiddleware
),如下所示:
AfterCacheMiddleware
class AfterCacheMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
$key = $this->keygen($request->url());
if( ! Cache::has( $key ) ) Cache::put( $key, $response->getContent(), 1 );
return $response;
}
protected function keygen( $url )
{
return 'route_' . Str::slug( $url );
}
}
BeforeCacheMiddleware
namespace App\Http\Middleware;
use Closure;
use Cache;
use Str;
class BeforeCacheMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$key = $this->keygen($request->url());
if( Cache::has( $key ) ) return Cache::get( $key );
return $next($request);
}
protected function keygen( $url )
{
return 'route_' . Str::slug( $url );
}
}
此外,我已经在 app/Http/Kernel.php
'App\Http\Middleware\AfterCacheMiddleware',
'App\Http\Middleware\BeforeCacheMiddleware',
'cacheafter' => 'App\Http\Middleware\AfterCacheMiddleware',
'cachebefore' => 'App\Http\Middleware\BeforeCacheMiddleware',
下面是我的路线:
Route::get('middle', ['middleware' => 'cachebefore','cacheafter', function()
{
echo "success";
}]);
在第一个页面加载时,一切正常(因为页面没有被缓存)。
在随后的访问中,我收到此错误:ErrorException in VerifyCsrfToken.php line 76: Trying to get property of non-object
.
我认为错误源于这一行:
if( Cache::has( $key ) ) return Cache::get( $key );
但是我想不通
试试吧。
<?php namespace App\Http\Middleware;
use Closure;
use Cache;
use Illuminate\Support\Str;
use Event;
class AfterCacheMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
/* Event::listen('illuminate.query', function($query)
{
var_dump($query);
return false;
});*/
$response = $next($request);
$key = $this->keygen($request->url());
$key2 = $this->keygen($request->url()).'_2';
if( ! Cache::has( $key ) )
{
Cache::put( $key, $response->getContent(), 1 );
Cache::put( $key2, $response->headers, 1);
}
return $next($request);
}
protected function keygen( $url )
{
return 'route_' . Str::slug( $url );
}
}
<?php namespace App\Http\Middleware;
use Closure;
use Cache;
use Illuminate\Support\Str;
class BeforeCacheMiddleware {
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$key = $this->keygen($request->url());
$key2 = $this->keygen($request->url()).'_2';
if( Cache::has( $key )&& Cache::has( $key2 ) )
{
$content=Cache::get( $key );
$header=Cache::get( $key2 );
$header=explode('Cache-Control:',$header);
$header1=explode('Content-Type:',$header[1]);
$header2=explode('Date:',$header1[1]);
$header3=explode('Set-Cookie:',$header2[1]);
$CacheControl=trim($header1[0]);
$ContentType=trim($header2[0]);
$Date=trim($header3[0]);
$SetCookie=trim($header3[1]);
return response($content)
->header('Cache-Control',$CacheControl)
->header('Content-Type',$ContentType)
->header('Date',$Date)
->header('Set-Cookie',$SetCookie);
}
return $next($request);
}
protected function keygen( $url )
{
return 'route_' . Str::slug( $url );
}
}
不需要添加所有这些东西。如果你也想更新 header 信息,这些很好。
在你之前的cacheiddleware中:
if( Cache::has( $key ) ) return Cache::get( $key );
按照@Den 的建议修改上面的代码
if( Cache::has( $key ) )
{
$content=Cache::get( $key );
return response($content);
}
这会奏效。如果您有任何问题,请返回。