![](/img/trans.png)
[英]Phalcon - How to call a model in another controller? "Error: Class not found"
[英]Model cannot be found in Phalcon controller
我是Phalcon的新手並嘗試訪問控制器中的模型,但顯示如下錯誤:
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
值得一提的是,我使用Phalcon-dev-tools創建了這個項目,並且我使用phalcon serve命令運行它。
這是控制器:
<?php
use \settings\Settings;
class UserEndController extends \Phalcon\Mvc\Controller
{
public function homeAction()
{
$settings = Settings::findFirst(1);
}
}
這是loader.php
文件:
<?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();
這是config.php
文件:
<?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' => '/',
]
]);
您沒有發布您的目錄結構。 即使有registerDirs,你仍然需要使用PSR-4。
只需更好地使用registerNamespaces並使用PSR-4,就是這樣,你永遠不會遇到任何問題。
由於格式錯誤,可能無法識別您的import語句。 可能會將其識別為嘗試從計算機的根目錄導入,但情況並非如此。
<?php
// Importing settings folder at root of computer file system.
use /settings/Settings
?>
作為一般經驗法則,最好在所有導入中使用APP_PATH
和當前系統位置。 由於config.php中已有一堆路徑:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.