Answer the question
In order to leave comments, you need to log in
How to set up namespaces in Falcon?
Through registerDirs, the autoloader loads everything correctly, through namespaces it gives a dispatch error, I can’t understand what the problem is, the path is set to the same
index.php code
<?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();
<?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';
}
}
Answer the question
In order to leave comments, you need to log in
This is how it works for me:
$loader = new Loader();
$loader->registerDirs([
'../app/Controllers/',
'../app/Models/',
'../app/Libs/',
]);
$loader->registerNamespaces([
'App' => '../app/',
]);
$loader->registerFiles([ '../vendor/autoload.php' ]);
$loader->register();
pub/ (index.php)
app/ (controllers/, models/, etc)
<?php
/**
* Офис.Справка
*/
namespace App\Controllers\Office;
use App\Models\Faq;
use App\Models\FaqCats;
use Phalcon\DI;
class FaqController extends BaseController {
public function initialize() {
parent::initialize();
}
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question