问题在标题中:如何在Laravel 4中禁用CSRF令牌?
我知道在Laravel 5中,在中间件中使用变量$except很容易,但在Laravel 4中我找不到解决方案……
发布于 2017-03-01 14:14:10
一种方法是扩展VerifyCsrfToken,并在其中没有csrf的数组:
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Session\TokenMismatchException;
class VerifyCsrfToken extends \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken {
protected $except_urls = [
'contact/create',
'contact/update',
...
];
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;
}
}
并在内核中进行更改以指向新的中间件:
protected $middleware = [
...
'App\Http\Middleware\VerifyCsrfToken',
];
您可以在那里找到更多详细信息:
发布于 2017-03-01 14:17:29
你可以通过修改VerifyCrsfToken.php类和提供$openRoutes来做到这一点,是的,当你接触基类时,你就是在玩火。:)
//app/Http/Middleware/VerifyCsrfToken.php
//add an array of Routes to skip CSRF check
private $openRoutes = ['free/route', 'free/too'];
//modify this function
public function handle($request, Closure $next)
{
//add this condition
foreach($this->openRoutes as $route) {
if ($request->is($route)) {
return $next($request);
}
}
return parent::handle($request, $next);
}
https://stackoverflow.com/questions/42533932
复制相似问题