📜  Phalcon-配置

📅  最后修改于: 2020-10-21 05:26:23             🧑  作者: Mango


Web应用程序的config文件夹包含以下文件-

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

设定档

config.php

它包括根据目录路径进行的数据库连接和路由配置。

 [ 
      '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应用程序的目录。

registerDirs( [ 
      $config->application->controllersDir, 
      $config->application->modelsDir 
   ] 
)->register(); 

services.php

该文件关联了实现Web项目服务的所有功能。它实现了Phalcon \ Di接口。它还通过加载服务来实现对服务的依赖注入。

基本上,config文件夹中的services.php文件充当所有服务的容器。该接口有助于初始化所有服务,例如数据库连接,设置cookie,创建新会话或与NoSQL数据库连接。

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; 
});