简体   繁体   中英

how to integrate htmlpurifier with symfony2?

I am trying to integrate htmlpurifier into a symfony2 controller, but symfony2 assumes the class I am trying to instantiate is part of that vary controller, but it is not, it is an included class type frmo the htmlpurifier library.

Is there a way to escape the class name so that symfony2 doesn't look for it in the current namespace?

I suggest to use the bundle version of HTMLPurifier for symfony2

you can found it on gitHub : https://github.com/Exercise/HTMLPurifierBundle

it's pretty easy to install with composer

Require the bundle in your composer.json file:

{
    "require": {
         "exercise/htmlpurifier-bundle": "*",
    }
}

Install the bundle:

$ composer update exercise/htmlpurifier-bundle

Register the bundle app/AppKernel.php :

public function registerBundles()
    {
        return array(
            new Exercise\HTMLPurifierBundle\ExerciseHTMLPurifierBundle(),
            // ...
        );
    }

you can use it as a service in a controller :

$purifier = $this->container->get('exercise_html_purifier.default');
$clean_html = $purifier->purify($dirty_html);

or a filter in a twig template :

{{ text|purify }}

also a Form Data Transformer for the symfony2 form builder

it's all in the docs : https://github.com/Exercise/HTMLPurifierBundle

Oh, just found it.

Instead of

require_once dirname('_FILE_') . '/plugins/htmlpurifier/library/HTMLPurifier.auto.php';
$purifier = new HTMLPurifier();

I should put a leading backslash on the class name

require_once dirname('_FILE_') . '/plugins/htmlpurifier/library/HTMLPurifier.auto.php';
$purifier = new \HTMLPurifier();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM