PHP code example of stevenmaguire / laravel-middleware-csp

1. Go to this page and download the library: Download stevenmaguire/laravel-middleware-csp 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/ */

    

stevenmaguire / laravel-middleware-csp example snippets

 php
// within app/Http/Kernal.php

protected $routeMiddleware = [
    //
    'secure.content' => \Stevenmaguire\Laravel\Http\Middleware\EnforceContentSecurity::class,
    //
];
 php
// within app/Http/routes.php

Route::get('gallery', ['middleware' => 'secure.content'], function () {
    return 'pictures!';
});
 php
// within app/Http/routes.php

Route::get('gallery', ['middleware' => 'secure.content:flickr'], function () {
    return 'pictures!';
});
 php
// within app/Http/Controllers/GalleryController.php

public function __construct()
{
    $this->middleware('secure.content');
}
 php
// within app/Http/Controllers/GalleryController.php

public function __construct()
{
    $this->middleware('secure.content:google');
}
 php
// within app/Http/Controllers/GalleryController.php

public function __construct()
{
    $this->middleware('secure.content:google,flickr,my_custom');
}
 php
// within config/security.php

return [
    'content' => [
        'default' => '',
        'profiles' => [],
    ],
];

 php
// within config/security.php

return [
    'content' => [
        'default' => '',
        'profiles' => [
            'profile_one' => [],
            'profile_two' => [],
            'profile_three' => [],
        ],
    ],
];

 php
// within config/security.php

return [
    'content' => [
        'default' => 'global',
        'profiles' => [
            'global' => [
                'base-uri' => "'self'",
                'default-src' => "'self'",
                'font-src' => [
                    "'self'",
                    'fonts.gstatic.com'
                ],
                'img-src' => "'self'",
                'script-src' => "'self'",
                'style-src' => [
                    "'self'",
                    "'unsafe-inline'",
                    'fonts.googleapis.com'
                ],
            ],
            'flickr' => [
                'img-src' => [
                    'https://*.staticflickr.com',
                ],
            ],
        ],
    ],
];