I'd like to create my own helper but can't find any help on Google for Symfony 1.4/Doctrine.
I guess it has something to do with creating a myClassHelper.class.php
in lib/helpers/
or something, but I don't know what to implement, or if specific methods have to be overridden.
Any help would be appreciated!
Creating Helper Functions The Symfony Plugin provides parameter types and return types inference based on their usage with Symfony classes and functions. This lets you create your own helper functions that take or return a template, a service, an entity, a translation, a route, a form, or an event.
Helper classes are usually a sign of lack of knowledge about the Model's problem domain and considered an AntiPattern (or at least a Code Smell) by many. Move methods where they belong, e.g. on the objects on which properties they operate on, instead of collecting remotely related functions in static classes.
I don't think that anything changed regarding the helpers in 1.4. From the documentation(although 1.2):
Helper functions (regular PHP functions returning HTML code) should be saved in a file called
FooBarHelper.php
, where FooBar is the name of the helper group. Store the file in theapps/myapp/lib/helper/
directory (or in anyhelper/
directory created under one of thelib/
folders of your project) so it can be found automatically by theuse_helper('FooBar')
helper for inclusion.
So you just put normal functions in a normal file (has nothing to do with classes or methods). The functions can take arbitrary parameters (you decide what they need to create the HTML) and the have to return HTML.
E.g.
MyHelper.php
function hello_word() {
return '<strong>Hello world!</strong>';
}
and in the template:
<?php use_helper('My') ?>
<!-- somewhere in the template -->
<?php echo hello_world() ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With