0

So everything works fine if i use $loader->registerDirs() function, but if i use registerNamespaces function i get dispatcher error, that says: "IndexController handler class cannot be loaded". here is code for index.php file

<?php

use Phalcon\Di\FactoryDefault;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Url;
use Phalcon\Db\Adapter\Pdo\Mysql;

// 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/',
//        APP_PATH . '/modules/'
//    ]
//);

$loader->registerNamespaces(
    [
     'App\Controllers' =>   APP_PATH . '/controllers/'
    ]
);

$loader->register();


$container = new FactoryDefault();

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

$container->set(
    'db',
    function () {
        return new Mysql(
            [
                'host'     => 'localhost',
                'username' => 'sammy',
                'password' => 'password',
                'dbname'   => 'learning',
            ]
        );
    }
);

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

$application = new Application($container);


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

    $response->send();

That my IndexController.php file.

<?php
declare(strict_types=1);
namespace App\Controllers;

use Phalcon\Http\Request;
use \Phalcon\Mvc\Controller;
use Phalcon\Http\Message\Uri;

class IndexController extends Controller
{

    public function indexAction()
    {
        return 'hi';
    }
}

so as you can see the path for registerDirs and registerNamespaces functions are the same, but it doesn't seem to work. Because of it i tried many ways of changing paths but it didn't help.

Here's my directory structure: directory structure

Nate Rope
  • 73
  • 4

1 Answers1

0

in short you need to change the default namespace in Phalcon\Mvc\Router

$router = $container->getRouter();

$router->setDefaultNamespace('App\Controllers');

$router->handle();

you may want to read this answer https://stackoverflow.com/a/22673713/2640796

Talal
  • 394
  • 1
  • 13