Yii 1.1: Multiple Enviroments - Only with files

2 followers

The idea of ​​this wiki is to show how to have a development environment .

It is very likely that you have a GIT repository with your project, which loads on your production server, if not, as this guide may be useful: configuration database is not the same on your production server, in your production server see no runtime errors or other errors (for safety).

In your development environment you need to show errors, logs, and others, and do not want those files are uploaded to the repository or change parameters when you upgrade your application to the production server.

Bearing in mind these and other things, I've resolved as follows:

An index-dev.php file with the following code:

<?php
 
// change the following paths if necessary
$yii = dirname(__FILE__).'/../yii114/framework/yii.php';
$config = dirname(__FILE__).'/protected/config/main-dev.php';
 
// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG', true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL', 3);
 
require_once($yii);
Yii::createWebApplication($config)->run();

And this is the index.php file for server production:

<?php
 
// change the following paths if necessary
$yii = dirname(__FILE__).'/../yii/framework/yiilite.php';
$config = dirname(__FILE__).'/protected/config/main.php';
 
require_once($yii);
Yii::createWebApplication($config)->run();

As you can see on line 5 indicates that a configuration file is loaded: main-dev.php, that file is the one with all the settings for our development environment: database connection, Log, etc. The main-dev.php file is a copy of the file main.php, with some modifications as require.

Some code of my main-dev.php file:

'modules' => array(
  // uncomment the following to enable the Gii tool
  'gii' => array(
    'class' => 'system.gii.GiiModule',
    'password' => 'gii',
    'generatorPaths' => array(
      'application.modules.gii.generators',
    ),
    // If removed, Gii defaults to localhost only. Edit carefully to taste.
    'ipFilters' => array('127.0.0.1', '::1'),
  ),
),
 
'components' => array(
  'cache' => array(
    'class' => 'CFileCache'
  ),
  'urlManager' => array(
    'urlFormat' => 'path',
    'showScriptName' => true,
    'rules' => array(
      '<controller:\w+>/<id:\d+>' => '<controller>/view',
      '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
      '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    )
  ),
  'db' => array(
    'connectionString' => 'mysql:host=localhost;dbname=testyii',
    'emulatePrepare' => true,
    'username' => 'root',
    'password' => 'root',
    'charset' => 'utf8',
    'enableParamLogging' => true,
    'enableProfiling' => true,
  ),
  'log' => array(
    'class' => 'CLogRouter',
    'routes' => array(
      array(
        'class' => 'CFileLogRoute',
        'levels' => 'error, warning, info',
      ),
      array(
        'class' => 'CWebLogRoute',
        'showInFireBug' => false,
        'enabled' => false,
      ),
      array(
        'class' => 'ext.yii-debug-toolbar.YiiDebugToolbarRoute',
      )
    ),
  ),
)

And this is the main.php file for server production:

'modules' => array(
 
),
 
'components' => array(
  'cache' => array(
    'class' => 'CApcCache'
  ),
  'urlManager' => array(
    'urlFormat' => 'path',
    'rules' => array(
      '<controller:\w+>/<id:\d+>' => '<controller>/view',
      '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
      '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    )
  ),
  'db' => array(
    'connectionString' => 'mysql:unix_socket=/var/run/mysqld/mysqld.sock;dbname=testyii',
    'emulatePrepare' => true,
    'username' => 'other_username',
    'password' => 'other_password',
    'charset' => 'utf8',
    'schemaCachingDuration' => 31556926,
    'enableParamLogging' => false,
    'enableProfiling' => false,
  ),
  'log' => array(
    'class' => 'CLogRouter',
    'routes' => array(
      array(
        'class' => 'CFileLogRoute',
        'levels' => 'error, warning, info',
      ),
    ),
  ),
)

It is very important to note: If you are using urlManager set the parameter: showScriptName to TRUE to always make requests from index-dev.php

It is very basic but useful.

Be the first person to leave a comment

Please to leave your comment.

Write new article