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.

Micro routing, how to access $app etc when passing in handlers as array (class/method)

I know that I can use use with a closure but I am passing them in as array('fooController', 'barFunc'). Is there any way to access $app this way or am I stuck rewriting everything as a closure?



85.4k

If your controller extends Phalcon\Mvc\Controller you can access any service as follows:

class FooController extends \Phalcon\Mvc\Controller
{
    public function barFunc()
    {
        $data = $this->request->getPost();
    }
}


1.9k
edited Oct '14

Thats what I have. This is how I am routing to it :

$this->get('/foo', ['FooController', 'barFunc']);

//In barFunc

var_dump($this);

NULL

var_dump($this->request);

PHP message: PHP Fatal error:  Using $this when not in object context in ....


85.4k
Accepted
answer

An instance must be passed:

$fooController = new FooController;
$this->get('/foo', [$fooController, 'barFunc']);


1.9k

I tried the static string as well 'Controllers/FooController::barFunc' with same result. Will try your way.



1.9k

Thats perfect thanks!



1.9k

I see where I overlooked it in the documentation. Had been using a skeleton, I'll be sure to update the project owner.