cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

IndexController handler class cannot be loaded

Open garrett85 opened this issue 2 years ago • 5 comments

I'm having trouble, when I open my browser for my phalcon project I keep getting this error: IndexController handler class cannot be loaded

Notice that I've tried two different version of the echo $app->handle line

app/controllers/IndexController.php

<?php

class IndexController extends \Phalcon\Mvc\Controller
{
    public function indexAction()
    {
        echo "Hello World!";
    }
}

?>

public/index.php

<?php

try{
    // autoloader
    $loader = new \Phalcon\Loader();
    $loader->registerDirs([
        '../app/controllers/',
        '../app/models'
    ]);

    $loader->register();

    // dependency injection
    $di = new \Phalcon\DI\FactoryDefault();
    $di->set('view', function(){
        $view = new \Phalcon\Mvc\View();
        $view->setViewsDir('../app/views');

        return $view;
    });

    // deploy the app
    $app = new \Phalcon\Mvc\Application($di);
    $request = new Phalcon\Http\Request();
    // echo $app->handle($request->getURI())->getContent();
    echo $app->handle($_GET['_url'] ?? '/')->getContent();
}
catch(\Phalcon\Exception $e){
    echo $e->getMessage();
}

?>

garrett85 avatar Jul 04 '22 15:07 garrett85

Try this line

echo $this->app->handle($_SERVER['REQUEST_URI'])->getContent();

niden avatar Jul 04 '22 19:07 niden

Try this line

echo $this->app->handle($_SERVER['REQUEST_URI'])->getContent();

I tried that and got this: Fatal error: Uncaught Error: Using $this when not in object context in /var/www/html/learning-phalcon/public/index.php:27 Stack trace: #0 {main} thrown in /var/www/html/learning-phalcon/public/index.php on line 27

Also took away 'this->' and replaced it with $app and left the rest of your app untouched but it went back to: LearningPhalconController handler class cannot be loaded

garrett85 avatar Jul 08 '22 02:07 garrett85

$response =  $app->handle($_SERVER['REQUEST_URI']);
if (!$response->isSent()) {
      $response->send();
}

and please also inject your route service and set the default module/contorller.

zikezhang avatar Jul 18 '22 20:07 zikezhang

@garrett85 yes that should have been $app apologies.

    $loader->registerDirs([
        '../app/controllers/',
        '../app/models'
    ]);

This seems a bit suspicious. Is this the parent folder or where you are right now? ../app or ./app

niden avatar Sep 06 '22 00:09 niden

It might also be better to diagnose this if you set up a controller directory with namespaces and then use the loader to figure things out on its own. Just a thought

niden avatar Sep 06 '22 00:09 niden

If you are using v5, enable debugging in your loader and print out the debug information. That will give you more information on where the loader goes to find the files necessary

https://docs.phalcon.io/5.0/en/autoload#debugging

niden avatar Oct 12 '22 20:10 niden

Please try to use v5 and if issue persists - reopen the issue. Closing.

Jeckerson avatar Oct 26 '22 16:10 Jeckerson