Hey there, You might run into a circumstance where you need to build a json file with the data you need to fetch from the server, then read that file. How would you approach this situation or how might we approach it in the Laravel application? This example is for you if you don't know how to create a.json file and write data into it using Laravel.
Let's look at some sample code that demonstrates how to build a dynamic json file and then write it to our laravel application with dynamic data.
Global CSRF protection is disabled in Laravel.
Go to app/HTTP/ and then open the file Kernal.php. And under app/Http/Kernel.php, delete or comment out the line that reads "App/Http/Middleware/VerifyCsrfToken::class" as shown below:
App\Http\Kernel.php
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
//\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
//\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
For particular routes, Laravel disables CSRF token protection.
Follow the steps below to disable csrf protection for a given route. Simply open the path file listed below and make the necessary updates. Assume you have a few routes like the ones below
routes\web.php
Route::post('route-one', 'TestController@one');
Route::post('route-two', 'TestController@two');
Route::post('route-three', 'TestController@three');
Next, update the file like below. Than you will be able to disable csrf token for specific routes in your laravel application.
App\Http\Middleware\VerifyCsrfToken.php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* Indicates whether the XSRF-TOKEN cookie should be set on the response.
*
* @var bool
*/
protected $addHttpCookie = true;
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = ['route-one', 'route-three'];
}