Fil*_*ano 4 php laravel laravel-5
我正在尝试使用我的 Laravel 应用程序创建一个 api,但是当我对路由发出 post 请求时,Laravel 默认情况下会尝试验证 csrf 令牌。所以,我想删除 api 路由的这个验证。我想维护前端请求的验证。但是当我在 app/Http/Middleware/VerifyCsrfToken.php 中添加异常路由时,我收到了这个错误:
block_exception clear_fix
Run Code Online (Sandbox Code Playgroud)
这是VerifyCsrfToken.php
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
//
'log_bounces_complaints',
];
}
Run Code Online (Sandbox Code Playgroud)
只需扩展 VerifyCsrfToken 并添加要排除的 url。
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Session\TokenMismatchException;
class VerifyCsrfToken extends \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken {
protected $except_urls = [
'your_specific_url/new_url',
'your_specific_url/new_url_2',
...
];
public function handle($request, Closure $next)
{
$regex = '#' . implode('|', $this->except_urls) . '#';
if ($this->isReading($request) || $this->tokensMatch($request) || preg_match($regex, $request->path()))
{
return $this->addCookieToResponse($request, $next($request));
}
throw new TokenMismatchException;
}
}
Run Code Online (Sandbox Code Playgroud)
并在内核中,更改新的中间件。
protected $middleware = [
...
'App\Http\Middleware\VerifyCsrfToken',
];
Run Code Online (Sandbox Code Playgroud)