Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

Right place for shared custom functions

edited March 2015 in Framework

I would like to create some functions usable in all apps; for example some filters to use in views. What is the place/way to do it. Thanks.

Comments

  • The best way is to create a class in your app like this:

    <?php
    
    namespace myapp;
    
    class Filter {
        public static function my_filter ($data) {
            return strrev ($data);
        }
    }
    

    Then you can access it as a filter via {{ some_value|myapp\Filter::my_filter }}. The namespace and class wrapper make it accessible via the autoloader, so no includes necessary.

  • Create a "bootstrap.php" in root directory, this file will be required autoly. If you really like to define normal functions, you can put codes there.

  • Thanks to both.

Sign In or Register to comment.