Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
PHP OOP novice question in fuel
How I can share 2 methods in a class from backend and frontend.
For exemple I create a Controller_Post extends Controller_Base_Template with my 2 methods
then I use them from :
• Controller_Backend_Post extends from
rontend_Post extends from
what is best strategy, should instantiate class ?
We don't have non-protected/private methods in controllers, or in other words, all non-action methods in a controller are support methods for actions in that same controller.
For shared methods, we either use separate helper classes, usually with a static interface, or more recently traits if we want them available in multiple controllers.
p.s. for new apps, I suggest not using the underscore naming method anymore, but use namespaces, so \Controller\Base\Backend instead of
Controller_Base_Backend. The first one is PSR compliant, the second one will be deprecated with the release of 1.9 in a preparation to move fully to PSR-4 and composer.
great informations Thanks Harro.
I Moved the 2 methods in Template controller like the frontend and backend extends from template, so they share the same functions.
It works nice.
I can change All the namespace to
with my actual 1.8.1 ?
Yes, PSR-4 is fully supported in 1.8, but you do need to change "controller_prefix" in your config to
* Controller class prefix
'controller_prefix' => 'Controller\\',
We've been using this in our apps since early 2015...
Great, thanks for the tip.
Add a Comment
It looks like you're new here. If you want to get involved, click one of these buttons!
Apply for Membership
↳ Job Board
↳ Installation & Setup
↳ Tips and Tutorials
↳ Code share
In this Discussion