Love Fuel?    Donate

FuelPHP Forums

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 Controller_Base_Backend
    • Controller_Frontend_Post extends from Controller_Base_Frontend

    what is best strategy, should instantiate class ?

  • HarroHarro
    Accepted Answer
    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 PSR-4  with my actual 1.8.1 ?
  • HarroHarro
    Accepted Answer
    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. :)

Howdy, Stranger!

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

In this Discussion

  • efx September 2018
  • Harro September 2018