XF 2.1 Utility or Helper Class for use in multiple controllers

asprin

Active member
I have some controllers used by my add-on at the following locations:
Code:
src/addons/FC/Pub/Controller/FileA.php
src/addons/FC/Pub/Controller/FileB.php
src/addons/FC/Pub/Controller/FileC.php

If I were to have a helper/utility class that would do some common tasks such as convert timestamp to local, validate something etc..where can I place that? Would the following be a good option?
Code:
src/addons/FC/Util/MyClass.php // using the folder name as Util as I see the same being used in core XF framework

If so, how would I reference that class/method in my controller functions?
 
What happens when it is converted?


Services.
Just used as an example. Main intent is that it contains re-usuable functions to be called across multiple controllers.

Anyways, I found answer to the second part of my question (how to reference it inside the controller). Just need confirmation if the location of src/addons/<addon-ID>/Util would be a good enough place.

Just for reference purpose, the way to use it in controllers is as follows:
Code:
\FC\Util\Test::doSomething(); // provided doSomething is a static function
 
Just used as an example.
Then it really depends on what you're trying to do with the converted data. If something requires validations or too much value setting then it is a good idea to use services over Util.

Just for reference purpose, the way to use it in controllers is as follows:
Code:
\FC\Util\Test::doSomething(); // provided doSomething is a static function
Correct.
 
Back
Top Bottom