Hello to all fans of the Yii framework!
In a series of articles, I want to share our approach to the development of projects on Yii. Maybe our ideas will be useful to someone, maybe for someone it will be food for thought, well, and maybe someone doesn’t need this :). In no case do I pretend to be unique, do not claim that it should be done exactly as we do.
The first post will be about the configuration of the project, I understand, the topic is beaten, well, where can I go without it.
So, let's begin. First we analyze the input script (index.php). Here is what he looks like with us:
<?php $rootPath = dirname(__FILE__); $env = null; $envFile = $rootPath . '/.env'; if (is_file($envFile)) { $env = trim(file_get_contents($envFile)); } $configFile = $rootPath . '/application/config/production.php'; if (!empty($env)) { $configFile = $rootPath . '/application/config/' . $env . '.php'; if (!file_exists($configFile)) { die('Config file is not found.'); } } $config = require $configFile; $libraryPath = $rootPath . '/library'; require_once $libraryPath . '/yii/yii.php'; $autoloader = function($class) { global $libraryPath; $path = $libraryPath . '/' . implode(preg_split('/[_\\\]/', (string)$class, -1, PREG_SPLIT_NO_EMPTY), DIRECTORY_SEPARATOR) . '.php'; if (is_file($path)) { include_once $path; } }; Yii::registerAutoloader($autoloader, true); Yii::createWebApplication($config)->run();
The first thing we do here is define the application configuration. The path to the configuration is obtained on the basis of the contents of the file ".env" which lies right there in the root. If this file does not exist, then the config is “production”. Those. the file ".env" should only be on your machine, we don’t upload it to the combat server, put it to ignore, etc. What is all this for? - to separate the configuration of the production server and the developer server.
')
Plus in the config, I also define the corresponding constants. For example, for server devs, this is:
define('YII_DEBUG', true); define('YII_TRACE_LEVEL', 3); define('YII_ENABLE_ERROR_HANDLER', true); define('YII_ENABLE_EXCEPTION_HANDLER', true);
Why in the config file, and not in the "index.php"? - because the values ​​of the constants also depend on the environment.
Further, for convenience, you can make a merge of configurations. Those. select one basic (main) and specific (production, dev, test, etc.) that determine the features of the environment.
In this case, the contents of the “config” folder will be something like this:
- main.php // main application settings
- production.php // differences for production server
- dev.php // differences for developer server
Example, the content of the file "dev.php":
define('YII_DEBUG', true); define('YII_TRACE_LEVEL', 3); define('YII_ENABLE_ERROR_HANDLER', true); define('YII_ENABLE_EXCEPTION_HANDLER', true); return array_replace_recursive( require dirname(__FILE__) . '/main.php', array( 'components' => array( 'db' => array(
I note that we do the merging, using the native PHP function “array_replace_recursive ()”, and not using the “CMap :: mergeArray ()” framework (as recommended by the dollbook). Remember that at this moment we have not yet connected the library Yii. And they didn’t connect it, because it will cause a conflict between constants defined in our config and constants defined in yii.php.
If you look at the contents of “index.php”, then you will notice that we have slightly moved away from the standard naming of the root directories adopted in Yii. At the root we have only 3 folders:
- application // same as protected
- library // here are used libraries, including Yii
- public // files that are accessible from the web, for example css, js, images
- assest // folder for assets files
For the most part, such a structure is a matter of habit remaining from Zend.
In order for our assets to be added to “public / assets” in the config in the “assetManager” component, you need to override the “basePath” and “baseUrl” properties.
We look further. In “index.php” we define and register an additional autoloader. What for? In order to seamlessly connect additional libraries from the directory "library". Those. Our autoloader searches for the file in the “library” folder by the name of the corresponding class and connects it. Suppose for the class “Zend_Paginator” the path will be “library / Zend / Paginator.php”. He also understands the namespaces: for the class “Zend \ Paginator \ Paginator”, the path will be “library / Zend / Paginator / Paginator.php,” respectively. Do not worry, it will not affect performance, because Our autoloader will be activated after autoloader Yii. This behavior is defined by the second variable in "Yii :: registerAutoloader ()".
Well and at the end we start the application with an array of our configuration data.
That's all for today. Have a nice day everyone!
PS In my opinion, a good version of the separation of configs (via APPLICATION_ENV), offers suver -
habrahabr.ru/post/146473 . The main difference of my method is that it is not necessary to make the settings of the site in Apache, it is convenient in case we put the site in the localhost folder (http: // localhost / mysite)