We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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.5k
Accepted
answer

Ok, got it working now:

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