Yii Tutorial on Yii Entry Scripts

entry scripts are responsible for starting a request handling cycle. they are just php scripts accessible by users.

the following illustration shows the structure of an application −

entry scripts structure

web application (as well as console application) has a single entry script. the end user makes request to the entry script. then the entry script instantiates application instances and forwards requests to them.

entry script for a console application is usually stored in a project base path and named as yii.php. entry script for a web application must be stored under a web accessible directory. it is often called index.php.

the entry scripts do the following −

  • define constants.
  • register composer autoloader.
  • include yii files.
  • load configuration.
  • create and configure an application instance.
  • process the incoming request.

the following is the entry script for the basic application template −

<?php
   //defining global constants
   defined('yii_debug') or define('yii_debug', true);
   defined('yii_env') or define('yii_env', 'dev');
 
   //register composer autoloader
   require(__dir__ . '/../vendor/autoload.php');
   //include yii files
   require(__dir__ . '/../vendor/yiisoft/yii2/yii.php');
  
   //load application config
   $config = require(__dir__ . '/../config/web.php');
  
   //create, config, and process reques
   (new yii\web\application($config))->run();
?>

the following is the entry script for the console application −

#!/usr/bin/env php
<?php
   /** 
   * yii console bootstrap file. 
   * @link http://www.yiiframework.com/ 
   * @copyright copyright (c) 2008 yii software llc 
   * @license http://www.yiiframework.com/license/ 
   */
   //defining global constants
   defined('yii_debug') or define('yii_debug', true);
  
   //register composer autoloader
   require(__dir__ . '/vendor/autoload.php');
   require(__dir__ . '/vendor/yiisoft/yii2/yii.php');
  
   //load config
   $config = require(__dir__ . '/config/console.php');
  
   //apply config the application instance 
   $application = new yii\console\application($config);  

   //process request
   $exitcode = $application->run();
   exit($exitcode);
?>

the best place for defining global constants is entry scripts. there are three supported by yii constants −

  • yii_debug − defines whether you are in debug mode or not. if set to true, then we will see more log data and detail error call stack.

  • yii_env − defines the environment mode. the default value is prod. available values are prod, dev, and test. they are used in configuration files to define, for example, a different db connection (local and remote) or other values.

  • yii_enable_error_handler − specifies whether to enable the default yii error handler.

to define a global constant the following code is used −

//defining global constants 
defined('yii_debug') or define('yii_debug', true); 
which is equivalent to: 
if(!defined('yii_debug')) { 
   define('yii_debug', true); 
} 

note − the global constants should be defined at the beginning of an entry script in order to take effect when other php files are included.