PHP code example of depthbomb / csrf-bundle

1. Go to this page and download the library: Download depthbomb/csrf-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

depthbomb / csrf-bundle example snippets


return [
    // Your other bundles...
    Depthbomb\CsrfBundle\CsrfBundle::class => ['all' => true],
];



namespace App\Controller;

use Depthbomb\CsrfBundle\Attribute\CsrfProtected;
use Symfony\Component\HttpFoundation\Response;

class ProfileController
{
    #[CsrfProtected('update-profile')]
    public function update(): Response
    {
        // Update the profile after the token check succeeds.
        return new Response('Profile updated');
    }
}

$token = $tokenManager->getToken('update-profile')->getValue();

#[CsrfProtected('update-profile', tokenField: '_token', queryParameter: null)]

#[CsrfProtected(
    'update-profile',
    tokenField: null,
    queryParameter: null,
    header: 'X-CSRF-Token',
)]

#[CsrfProtected('update-profile', methods: ['POST', 'PUT', 'PATCH', 'DELETE'])]

use Symfony\Component\Security\Http\Attribute\IsCsrfTokenValid;

#[IsCsrfTokenValid('update-profile', tokenKey: '_csrf_token')]

#[IsCsrfTokenValid(
    'update-profile',
    tokenKey: 'X-Csrf-Token',
    tokenSource: IsCsrfTokenValid::SOURCE_HEADER,
)]