Phalcon 4 documentation controller handler issue

≯℡__Kan透↙ 提交于 2021-02-08 10:18:42

问题


I'm Using Phalcon 4.0.6 on windows 10,x64bit with psr & php version is 7.4.7. I follow basic tutorial example from "https://docs.phalcon.io/4.0/en/tutorial-basic" but I'm getting error like: "Exception: SingleController handler class cannot be loaded". Is it phalcons problem or am i doing anything wrong?

File Structure:

[Bootstrap]

<?php

use Phalcon\Loader;
use Phalcon\Di\FactoryDefault;
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();
}

[IndexController]

<?php

use Phalcon\Mvc\Controller;

class IndexController extends Controller
{

    public function indexAction()
    {
        return '<h1>Hello</h1>';
    }
}

回答1:


I'm not sure of your environment, but this error message appears when you mistake the controller's path or name.

You changed or added the SingleController.php into your controller’s path, right? That isn't on the tutorial.

You should check that your Bootstrap file has access to the controller’s path (or SingleController.php).




回答2:


I just did the basic tutorial with php 7.4.6 - but with apache webserver:

My Question to you: Did you start your page with http://localhost/ ? so there should not be any "SingleController"-Error to be run into :-/

At the basic tutorial phalcon does not do that much - just putting all steps together for displaying your hello world from your IndexController.php (in app/controllers/ ) because you don't work with models nor views nor templates at all.

if you use apache as well, take care that your DocumentRoot is "[WebProjectDir]/public/" -> if you have apache standard install it could be: "C:\apache\htdocs\public" (Windows Style) and in Apache use unix-style ;)

That could be a bit tricky at the beginning :)

Okay, maybe the Documentation can be corrected at some steps - for example the integration of PHPStorm is quite easyer than shown in video. maybe I'll correct that at some time :)

And now have fun with Phalcon, come back on any questions :)




回答3:


the issue because the project is not on root folder

the best solution to this issue is reverse every change you made before ( judging by reading some of the comments ) which suggests editing httpd.conf which is not recommended mostly because it is the same as moving the contents of folder single to htdocs/ and this will get your server stuck on one project

now the simple solution:

edit single/public/index.php

change $_SERVER['REQUEST_URI'] to $_GET['_url'] ?? '/'

like so:

echo $application->handle($_GET['_url'] ?? '/')->getContent();


来源:https://stackoverflow.com/questions/62391836/phalcon-4-documentation-controller-handler-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!