I'm new to Phalcon and trying to access a model in a controller but the error below is shown:
Fatal error: Uncaught Error: Class 'settings\Settings' not found in C:\xampp\htdocs\icriticize\app\controllers\UserEndController.php:11 Stack trace: #0 [internal function]: UserEndController->homeAction() #1 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(UserEndController), 'homeAction', Array) #2 [internal function]: Phalcon\Dispatcher->dispatch() #3 C:\xampp\htdocs\icriticize\public\index.php(42): Phalcon\Mvc\Application->handle() #4 C:\xampp\htdocs\icriticize\.htrouter.php(30): require_once('C:\\xampp\\htdocs...') #5 {main} thrown in C:\xampp\htdocs\icriticize\app\controllers\UserEndController.php on line 11
And to be mentioned I created this project using Phalcon-dev-tools and I'm running it using phalcon serve command.
This is the controller:
<?php
use \settings\Settings;
class UserEndController extends \Phalcon\Mvc\Controller
{
public function homeAction()
{
$settings = Settings::findFirst(1);
}
}
This is the loader.php
file:
<?php
$loader = new \Phalcon\Loader();
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
[
$config->application->controllersDir,
$config->application->modelsDir
]
)->register();
And this is the config.php
file:
<?php
/*
* Modified: prepend directory path of current file, because of this file own different ENV under between Apache and command line.
* NOTE: please remove this comment.
*/
defined('BASE_PATH') || define('BASE_PATH', getenv('BASE_PATH') ?: realpath(dirname(__FILE__) . '/../..'));
defined('APP_PATH') || define('APP_PATH', BASE_PATH . '/app');
return new \Phalcon\Config([
'database' => [
'adapter' => 'Mysql',
'host' => 'localhost',
'username' => 'root',
'password' => '',
'dbname' => 'icriticize',
'charset' => 'utf8',
],
'application' => [
'appDir' => APP_PATH . '/',
'controllersDir' => APP_PATH . '/controllers/',
'modelsDir' => APP_PATH . '/models/',
'migrationsDir' => APP_PATH . '/migrations/',
'viewsDir' => APP_PATH . '/views/',
'pluginsDir' => APP_PATH . '/plugins/',
'libraryDir' => APP_PATH . '/library/',
'cacheDir' => BASE_PATH . '/cache/',
// This allows the baseUri to be understand project paths that are not in the root directory
// of the webpspace. This will break if the public/index.php entry point is moved or
// possibly if the web server rewrite rules are changed. This can also be set to a static path.
'baseUri' => '/',
]
]);
You didn't post your directory structure. Also even though there is registerDirs you still need to use PSR-4.
Just better use registerNamespaces and use PSR-4 and that's it, with this you will never get any problem.
It's likely that your import statement is not recognized because the formatting is wrong. It may be recognized as attempting to import from the root directory of the computer, which should not be the case.
<?php
// Importing settings folder at root of computer file system.
use /settings/Settings
?>
As a general rule of thumb, it's best to use the APP_PATH
and current system locations in all imports. Since you already have a bunch of paths in your config.php: