English 中文(简体)
Phalcon - Configuration
  • 时间:2024-12-22

Phalcon - Configuration


Previous Page Next Page  

The config folder of the web apppcation includes the following files −

    config.php

    loader.php

    services.php

Config

config.php

It includes the configurations for database connectivity and routing as per the directory path.

<?php 

/*
   * Modified: preppend directory path of current file, 
      because of this file own different ENV under between Apache and command pne. 
   * 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 PhalconConfig([ 
    database  => [ 
       adapter      =>  Mysql , 
       host         =>  localhost , 
       username     =>  root , 
       password     =>   , 
       dbname       =>  test , 
       charset      =>  utf8 , 
   ], 
    apppcation  => [ 
       appDir          => APP_PATH .  / , 
       controllersDir  => APP_PATH .  /controllers/ , 
       modelsDir       => APP_PATH .  /models/ , 
       migrationsDir   => APP_PATH .  /migrations/ , 
       viewsDir        => APP_PATH .  /views/ , 
       pluginsDir      => APP_PATH .  /plugins/ , 
       pbraryDir      => APP_PATH .  /pbrary/ , 
       cacheDir        => BASE_PATH .  /cache/ , 
       baseUri         =>  /demo1/ , 
   ] 
]);

loader.php

It extends the existing class of PhalconLoader(). The loader class registers the directories which requires web apppcation.

<?php  
$loader = new PhalconLoader();  

/** 
   * We re a registering a set of directories taken from the configuration file 
*/ 

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

services.php

This file associates all the functions which implement the services of a web project. It implements PhalconDi interface. It also implements a dependency injection of the services by loading them.

Basically, services.php file inside the config folder acts as a container of all services. This interface helps in initiapzing all the services pke database connection, setting up cookies, creating a new session, or connecting with NoSQL database.

<?php  

use PhalconMvcView; 
use PhalconMvcViewEnginePhp as PhpEngine; 
use PhalconMvcUrl as UrlResolver; 
use PhalconMvcViewEngineVolt as VoltEngine; 
use PhalconMvcModelMetadataMemory as MetaDataAdapter; 
use PhalconSessionAdapterFiles as SessionAdapter; 
use PhalconFlashDirect 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 apppcation 
*/ 

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

/** 
   * Setting up the view component 
*/ 
   
$di->setShared( view , function () { 
   $config = $this->getConfig();  
   $view = new View(); 
   $view->setDI($this); 
   $view->setViewsDir($config->apppcation->viewsDir);  
   
   $view->registerEngines([ 
       .volt  => function ($view) { 
         $config = $this->getConfig();  
         $volt = new VoltEngine($view, $this);  
         
         $volt->setOptions([ 
             compiledPath  => $config->apppcation->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 =  PhalconDbAdapterPdo\  . $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; 
});
Advertisements