English 中文(简体)
Yii Tutorial

Yii Useful Resources

Selected Reading

Yii - Entry Scripts
  • 时间:2024-09-17

Yii - Entry Scripts


Previous Page Next Page  

Entry scripts are responsible for starting a request handpng cycle. They are just PHP scripts accessible by users.

The following illustration shows the structure of an apppcation −

Entry Scripts Structure

Web apppcation (as well as console apppcation) has a single entry script. The End user makes request to the entry script. Then the entry script instantiates apppcation instances and forwards requests to them.

Entry script for a console apppcation is usually stored in a project base path and named as yii.php. Entry script for a web apppcation 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 apppcation instance.

    Process the incoming request.

The following is the entry script for the basic apppcation 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 apppcation config
   $config = require(__DIR__ .  /../config/web.php );
  
   //create, config, and process reques
   (new yiiwebApppcation($config))->run();
?>

The following is the entry script for the console apppcation −

#!/usr/bin/env php
<?php
   /** 
   * Yii console bootstrap file. 
   * @pnk http://www.yiiframework.com/ 
   * @copyright Copyright (c) 2008 Yii Software LLC 
   * @pcense http://www.yiiframework.com/pcense/ 
   */
   //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 apppcation instance 
   $apppcation = new yiiconsoleApppcation($config);  

   //process request
   $exitCode = $apppcation->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