5

What is the best way to put custom library or helper methods in symfony? I am using doctrine with my project. One place I consider to put is under

project_root/lib/vendor/MyClasses/
But if I want to create a class or helper function which will use some core symfony/doctrine methods and return a result then how to do that and where should I put it? I want it to call from different modules to avoid code duplication.
med
  • 369
  • 1
  • 4
  • 18

2 Answers2

7

As for the custom library part of the question, you might probably want to put your external library into the lib/vendor folder. Since symfony doesn't automatically load everything that's in there, you'll have to tell its autoloader to do so.

You can either extend your config/ProjectConfiguration.class.php (as described here) or (and this is the much simpler and cleaner way) you add them to your config/autoload.yml (you might have to create this one). For the latter option, this is a great place to start looking at.

Burgi
  • 1,385
  • 9
  • 13
  • Thanks for you answer. Both of your links pointing to the same url. Can you please check. So putting it inside any of the lib/ directory will autoload the classes? But do I need to extend my custom classes from any symfony/doctrine classes to have them able to access doctrine/symfony core features? – med Mar 07 '11 at 09:03
  • @medhad Link 1 fixed. No, as I mentioned, _/lib/vendor/_ is the best place to put your external libraries, but with the standard config, only the _lib/vendor/symfony_ folder is being autoloaded (see above). If you want to make use of symfony functionality, you might want to look into wrapping both up into a plugin. A bit of googleing will certainly take you further. – Burgi Mar 07 '11 at 14:01
  • 1
    Just adding my findings so it may help others who are reading this! After trying different approaches what I have found that it is also possible to use a class by creating or putting it inside the **app/lib/** directory(app specific) or **app/module/lib** directory(for module specific) of your Symfony project. Symfony will automatically load the classes and you can call those from your action. You may extend it from sfActions or not; but doing so you will be able to access some core features like `sfContext::getInstance()` and similar other class methods and properties. – med Mar 20 '11 at 11:46
2

It seems to be a duplicate question.

As asked in symfony's 1.2 "Definitive Guide" docs :

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 the apps/myapp/lib/helper/ directory (or in any helper/ directory created under one of the lib/ folders of your project) so it can be found automatically by the use_helper('FooBar') helper for inclusion.

So, if you want to create custom helper FooBar for foo() function, create file lib/helper/FooBarHelper.php :

function foo() {echo "foo!"; }

to use it in your template:

use_helper('FooBar')
....

foo(); //outs "foo!"
Community
  • 1
  • 1
sumkincpp
  • 161
  • 3
  • But how can I use it from the module action not from view. Suppose I want to get a variable returned by Class/helper method containing the result set. It seems helper can be called from the template only and it must generate some html output. I want to create a class with some method that I can call from different module action. Which class to extend and where to put it? – med Mar 07 '11 at 06:52