We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to call a helper function in a controller

Hi I have created a file Helper.php in the helper folder it contains the code:

class Helper extends Phalcon\Mvc\User\Component
{

    public function GetMessage($code)
    {

        $message = $this->modelsManager
            ->createBuilder()
            ->columns([
                'message.msg_type',
                'message.msg',
            ])
            ->from(['message' => 'SystemMessage'])
            ->where('message.msg_code = :msg_code:', 
                ['msg_code' => $code])
            ->limit(1)
            ->getQuery()
            ->getSingleResult();

            return $message;
            }
    }

I have added to loader.php: $config->application->helpersDir,

and to config.php: 'helpersDir' => APP_PATH . '/helpers/',

How do I actually now call the function with in a controller I have tried quite a few ways without success.

Secondly is this the best way to do this? Is there a better recommended way of setting up this global function? (in terms of best practice)

Thanks



6.6k
Accepted
answer

Ok, got it working now:

$this->view->disable(); $msg = new Helper; $message = $msg->GetMessage('ER_CARD_DECLINED');