0

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: enter image description here

[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>';
    }
}
Styled Bee
  • 141
  • 1
  • 12

3 Answers3

0

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).

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • I have No SingleController.php. I have Two file One is bootstrap(index.php) and another is IndexController.php. Please see attached image of file structure. – Styled Bee Jun 20 '20 at 17:46
  • Thanks, your bootstrap and IndexController worked normally in my environment. Which path did you access? This tutorial doesn't have a router file and a router works automatically according to your access path. That is, IndexController::indexAction() works when you access to http:/your_env/ but, you need to create SingleController::indexAction() when you need to access to http:/your_env/single/. – Akira Kato Jun 22 '20 at 06:27
0

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 :)

Marco
  • 1
  • Oh, and forgotten to mention - if you start and you do not like command line - enable the webtools - they are fantastic! :) – Marco Jun 19 '20 at 10:47
  • Single is my project file name. I add the file structure image – Styled Bee Jun 19 '20 at 14:05
  • 1
    Cool, so, problem nearly solved - might you start with index.php? How do you start the site in the browser? (--> apache or php builtin? if php -> which command line? can you edit your post and give us your .htrouter-file? if apache -> what is your DocumentRoot and Directory in httpd.conf?) you are close to the goal :) – Marco Jun 20 '20 at 08:00
  • I'm on Windows using xampp. My Document root is htdocs. – Styled Bee Jun 20 '20 at 14:05
  • cool, so now one small change: find c:\XAMPP\apache\conf\httpd.conf • search for >>DocumentRoot<< • it looks like "DocumentRoot "C:/xampp/htdocs"" • set the value to "C:/xampp/htdocs/single/public/" • same with – Marco Jun 22 '20 at 08:23
  • I just Solved the problem by adding router in my bootstrap. I don't understand why i need to edit httpd.conf? – Styled Bee Jun 23 '20 at 17:45
  • Cool. ;) Okay, the thing with the public-path has a security reason. you hide your application code from your visitors - imho this is a great security feature of phalcon so it is a second great thing for minimizing the risc of being hacked or so. The first great thing is the binary file so attacker have to know phalcon and can't read any source if they reach to the app source what is quite more difficult (okay wait, isn't phalcon open source?). Okay, source! my source is: https://docs.phalcon.io/3.4/de-de/webserver-setup#document-root - have fun at flying with phalcon :) – Marco Jun 24 '20 at 19:57
0

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();
Talal
  • 394
  • 1
  • 13