Yii - Entry Scripts


Advertisements


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.



Advertisements
E-Books Store