I'm trying to create an api with my laravel app, but when I do a post request to a route, Laravel by default tries to verify the csrf token. So, I want to remove this verification for the api routes. I want to maintain the verification for the front end request. But when I add the exception routes in app/Http/Middleware/VerifyCsrfToken.php, I'm getting this error:
block_exception clear_fix
this is the 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',
];
}
Just extend the VerifyCsrfToken and add the urls you want to exclude.
<?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;
}
}
and in the Kernel, change the new middleware.
protected $middleware = [
...
'App\Http\Middleware\VerifyCsrfToken',
];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With