Phalcon-configuration

提供:Dev Guides
移動先:案内検索

ファルコン-設定

Webアプリケーションの設定フォルダには、次のファイルが含まれています-

  • config.php
  • loader.php
  • services.php

Config

config.php

これには、ディレクトリパスごとのデータベース接続とルーティングの構成が含まれます。

<?php

/*
 *Modified: preppend 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'      => 'test',
      '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/',
      'baseUri'        => '/demo1/',
   ]
]);

loader.php

  • \ Phalcon \ Loader()*の既存のクラスを拡張します。 ローダークラスは、Webアプリケーションを必要とするディレクトリを登録します。
<?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();

services.php

このファイルは、Webプロジェクトのサービスを実装するすべての機能を関連付けます。 Phalcon \ Di インターフェイスを実装します。 また、サービスをロードすることにより、サービスの依存性注入も実装します。

基本的に、configフォルダー内のservices.phpファイルは、すべてのサービスのコンテナーとして機能します。 このインターフェイスは、データベース接続、Cookieの設定、新しいセッションの作成、NoSQLデータベースとの接続など、すべてのサービスの初期化に役立ちます。

<?php

use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Php as PhpEngine;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Flash\Direct as Flash;

/* *
  * Shared configuration service
*/

$di->setShared('config', function () {
   return include APP_PATH . "/config/config.php";
});

/* *
  * The URL component is used to generate all kind of urls in the application
*/

$di->setShared('url', function () {
      $config = $this->getConfig();
      $url = new UrlResolver();
      $url->setBaseUri($config->application->baseUri);
   return $url;
});

/* *
  * Setting up the view component
*/

$di->setShared('view', function () {
   $config = $this->getConfig();
   $view = new View();
   $view->setDI($this);
   $view->setViewsDir($config->application->viewsDir);

   $view->registerEngines([
      '.volt' => function ($view) {
         $config = $this->getConfig();
         $volt = new VoltEngine($view, $this);

         $volt->setOptions([
            'compiledPath' => $config->application->cacheDir,
            'compiledSeparator' => '_'
         ]);
         return $volt;
      },
      '.phtml' => PhpEngine::class
   ]);
   return $view;
});

/* *
  * Database connection is created based in the parameters defined in the configuration
      file
*/

$di->setShared('db', function () {
   $config = $this->getConfig();
   $class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
   $connection = new $class([
      'host'     => $config->database->host,
      'username' => $config->database->username,
      'password' => $config->database->password,
      'dbname'   => $config->database->dbname,
      'charset'  => $config->database->charset
   ]);
   return $connection;
});