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

Call to method handle() on a non object

I hava a error,It says phalcon\Mvc\Application::handle(): Call to method handle() on a non object,my code like this

$application = new Application($di);
    // Register the installed modules
    $application->registerModules(
        array(
            'frontend' => array(
                'className' => 'Gkr\Frontend\Module',
                'path'      => '../apps/frontend/Module.php',
            ),
            'backend'  => array(
                'className' => 'Gkr\Backend\Module',
                'path'      => '../apps/backend/Module.php',
            )
        )
    );
    echo $application->handle()->getContent();

can you help me?

Hmm.. I have multiple modules in my app and I've never encountered this before.

I cannot even reproduce the same error. Anyone wanna take a bite on this?



96
Accepted
answer
edited Oct '14

i had the same problem.

changed the following:

$di->set('router', function() {
    $router = new Router();
    $router->add('/:controller/:action', array(
        'module' => 'test',
        'controller' => 1,
        'action' => 2,
    ));
});

to:

$di->set('router', function() {
    $router = new Router();
    $router->add('/:controller/:action', array(
        'module' => 'test',
        'controller' => 1,
        'action' => 2,
    ));
    return $router;
});

...and now it works.

edited Aug '20

In case it's helpful for anyone...

I found this post after my application (running Phalcon 4.0.6 on PHP 7.4.8) was telling me "Trying to call method handle on a non-object". The offending code in question was:

Container::$application->handle($_SERVER['REQUEST_URI']);

The strange thing was that Container::$application WAS indeed a Phalcon\Mvc\Application object as expected. I eventually narrowed down my problem to static closures used for several DI components. So, I'd have things like this:

Container::$di->setShared('view', static function () {
    $view = new Phalcon\Mvc\View();
    // ...
});

It seems Phalcon does NOT like those anonymous functions in its DI services. I didn't test each service individually (all of them used static closures), but when I converted everything to normal closures (i.e. without static), my problem went away. Like so:

Container::$di->setShared('view', function () {
    $view = new Phalcon\Mvc\View();
    // ...
});

Posting for anyone that runs into this, hopefully saving you some time.