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

$app and handlers

Hi, I have started to use Phalcon for an API.

I have build it as a Micro application and I am using handlers. The main issue for me is that I can't find how to access my app from the controllers since it's not injected like when you use closure. Anybody has any idea how to inject it. I can always define a parent to my controllers, define an injections method and do it just after I have instantiated my controller but that would mean I wouldn't be able to lazy load my controller and I am sure somebody had that issue before.

So I figured it out and should have thought a bit more about it before posting. I used the DependencyInjection by setting the app and retrieving it in the controller.



98.9k

You can create controllers that extend from Phalcon\DI\Injectable, so you can easily access the DI using magic getters:

class MyController extends Phalcon\DI\Injectable
{
    public function show()
    {
        $data = $this->request->getPost();
    }
}