bcosca / fatfree

A powerful yet easy-to-use PHP micro-framework designed to help you build dynamic and robust Web applications - fast!
2.66k stars 446 forks source link

Register token filter don't works #1219

Closed gmzu closed 3 years ago

gmzu commented 3 years ago

I create a new class, as explained in API reference

 class Helper extends \Prefab {
    function badwords($val) {
        $bad_words = array("badword","jerk","damn");
        $replacement_words = array("@#$@#", "j&*%", "da*@"); 
        return str_ireplace($bad_words, $replacement_words, $val);
    }
}

I later registered new filter in a controller class -> beforeRoute function with

\Preview::instance()->filter('badwords','\Helper::instance()->badwords');

In a template file I use {{@text | badwords}} but in the line in which I use the new filter I get an Invalid method NULL As if the registration of the filter had not happened No errors if I don't use the filter in the template Tested on php 7.3.9 and 7.4.2

ikkez commented 3 years ago

I guess it's just the call here... try \Preview::instance()->filter('badwords','Helper->badwords');

gmzu commented 3 years ago

it doesn't work

gmzu commented 3 years ago

After much testing I managed to get it to work with

\Template::instance()->filter('badwords','Helper->badwords')

gmzu commented 3 years ago

After other tests, I verified that it really only works like that (I was misled by the cache)

\Template::instance()->filter('badwords','Helper::badwords')

obviously you have to declare the functions as static. In the other case it always gives an invalid '->' object operator error in template temp file

KOTRET commented 3 years ago

makes sense as you do not want to create new instances all the time :)