Phalconでディレクトリを追加しエラーが出た場合の対応

Phalcon,PHP

 

エラーの発生

Phalconでディレクトリを新しく追加し、定数クラスを作成したのですが以下のエラーが出てきました。

Fatal error: Class 'HOGE' not found in ~ on line 8

対応策

その場合は追加したディレクトリを設定ファイルに追記すればOKです。
app/config/config.phpの下記の個所に追記します。

return new \Phalcon\Config([
    'database' =>
        [
            'adapter' => 'Mysql',
            'host' => '127.0.0.1',
            'username' => 'root',
            'password' => '',
            'dbname' => 'mini_bbs',
            '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/',
            'constDir' => APP_PATH . '/lib/const/', ← ここに新しいキー名で定義し、追加したディレクトリを追記
            // 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' => preg_replace('/public([\/\\\\])index.php$/', '', $_SERVER["PHP_SELF"]),
        ]
    ]
);

/app/config/loader.php
上記で追加したキー名を下記のように記載します。

$loader->registerDirs([
    $config->application->controllersDir,
    $config->application->modelsDir,
    $config->application->constDir ← config.phpで追記したキー名をこのように記載
])->register();

これで読み込まれました。

Phalcon,PHP