【问题标题】:Model cannot be found in Phalcon controller在 Phalcon 控制器中找不到模型
【发布时间】:2019-11-01 02:43:00
【问题描述】:

我是 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'        => '/',
    ]
]);

【问题讨论】:

    标签: php model phalcon


    【解决方案1】:

    您没有发布您的目录结构。此外,即使有 registerDirs,您仍然需要使用 PSR-4。

    最好使用 registerNamespaces 并使用 PSR-4 就可以了,这样你就不会遇到任何问题了。

    【讨论】:

    • 我应该如何使用 Phalcon。 BTW 文件夹结构是 phalcon-dev-tools 自己创建的一个
    【解决方案2】:

    很可能因为格式错误而无法识别您的导入语句。可能会被识别为试图从计算机的根目录导入,不应该是这样。

    <?php
    
    // Importing settings folder at root of computer file system. 
    use /settings/Settings
    
    ?>
    

    作为一般经验法则,最好在所有导入中使用APP_PATH 和当前系统位置。 因为您的 config.php 中已经有一堆路径:

    1. 您应该将设置目录添加为 config.php 中的路径。
    2. 然后将 config.php 导入您的控制器文件。
    3. 然后使用引用的目录。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 2014-11-30
      相关资源
      最近更新 更多