A simple Laravel 5 service provider for including the HTMLPurifier for Laravel 5.
The HTMLPurifier Service Provider can be installed via Composer by requiring the mews/purifier
package and setting the minimum-stability
to dev
(required for Laravel 5) in your
project's composer.json
.
{
"require": {
"laravel/framework": "~5.0",
"mews/purifier": "dev-master"
},
"minimum-stability": "dev"
}
Update your packages with composer update
or install with composer install
.
To use the HTMLPurifier Service Provider, you must register the provider when bootstrapping your Laravel application. There are essentially two ways to do this.
Find the providers
key in app/config/app.php
and register the HTMLPurifier Service Provider.
'providers' => array(
// ...
'Mews\Purifier\PurifierServiceProvider',
)
Find the aliases
key in app/config/app.php
.
'aliases' => array(
// ...
'Purifier' => 'Mews\Purifier\Facades\Purifier',
)
To use your own settings, you can publish configuration file to your application's config directory by running
$ php artisan vendor:publish
Make sure that you have registered Purifier service provider before you have executed vendor:publish command.
Example config file app/config/purifier.php
return array(
'encoding' => 'UTF-8',
'finalize' => true,
'preload' => false,
'settings' => array(
'default' => array(
'HTML.Doctype' => 'XHTML 1.0 Strict',
'HTML.Allowed' => 'div,b,strong,i,em,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]',
'CSS.AllowedProperties' => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align',
'AutoFormat.AutoParagraph' => true,
'AutoFormat.RemoveEmpty' => true,
),
"titles" => array(
'AutoFormat.AutoParagraph' => false,
'AutoFormat.Linkify' => false,
)
),
);
default
Purifier::clean(Input::get('inputname'));
dynamic config
Purifier::clean('This is my H1 title', 'titles');
Purifier::clean('This is my H1 title', array('Attr.EnableID' => true));