Phalcon 4.0 PHP - Controller handler class cannot be loaded

466 views Asked by At

I am trying to create a basic application with Phalcon locally. I followed the documentation (https://docs.phalcon.io/4.0/en/tutorial-basic) and I created the same file structure and I copied the code for all the files. Unfortunately, the error message 'Controller handler class cannot be loaded' is displayed every time. I tried to change the file path since I am working in Windows 10 x64, but it did not solve my problem. The index.php inside the public folder cannot find the controller class under the /app/controllers folder. I was thinking if there is any problem with my Phalcon installation, but in the phpinfo I can see the Phalcon plugin. phpinfo-phalcon

My code in the index.php file under public folder is:

<?php

use Phalcon\Di\FactoryDefault;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Url;

// Define some absolute path constants to aid in locating resources
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');

// Register an autoloader
$loader = new Loader();

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

$loader->register();

$container = new FactoryDefault();

$container->set(
    'view',
    function () {
        $view = new View();
        $view->setViewsDir(APP_PATH . '/views/');
        return $view;
    }
);

$container->set(
    'url',
    function () {
        $url = new Url();
        $url->setBaseUri('/');
        return $url;
    }
);

$application = new Application($container);

try {
    // Handle the request
    $response = $application->handle(
        $_SERVER["REQUEST_URI"]
    );

    $response->send();
} catch (\Exception $e) {
    echo 'Exception: ', $e->getMessage();
}

and my code for the IndexController.php file under app/controllers folder:

<?php

use Phalcon\Mvc\Controller;

class IndexController extends Controller
{
    public function indexAction()
    {
        return '<h1>Hello!</h1>';
    }
}

Finally, I added the .htrouting file in the same level with app and public folders which includes:

<?php

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

if ($uri !== '/' && file_exists(__DIR__ . '/public' . $uri)) {
    return false;
}

$_GET['_url'] = $_SERVER['REQUEST_URI'];

require_once __DIR__ . '/public/index.php';

the file structure is displayed here: file structure

I will appreciate any help, thank you. I am using WAMP with PHP 7.3.21, Apache 2.4.46 and the Phalcon plugin phalcon_x64_vc15_php7.4

1

There are 1 answers

0
CyberHunter On

I found a solution to this problem. So, I changed the part below in the index.php inside the public folder

try {
    // Handle the request
    $response = $application->handle(
        $_SERVER["REQUEST_URI"]
    );

to this

try {
    // Handle the request
    $response = $application->handle(
        $_GET['_url'] ?? '/'
    );

The answer was found in the phalcon forum https://forum.phalcon.io/discussion/20291/phalcon-4-new-project-error#C62007