- Yii Tutorial
- Yii - Home
- Yii - Overview
- Yii - Installation
- Yii - Create Page
- Yii - Application Structure
- Yii - Entry Scripts
- Yii - Controllers
- Yii - Using Controllers
- Yii - Using Actions
- Yii - Models
- Yii - Widgets
- Yii - Modules
- Yii - Views
- Yii - Layouts
- Yii - Assets
- Yii - Asset Conversion
- Yii - Extensions
- Yii - Creating Extensions
- Yii - HTTP Requests
- Yii - Responses
- Yii - URL Formats
- Yii - URL Routing
- Yii - Rules of URL
- Yii - HTML Forms
- Yii - Validation
- Yii - Ad Hoc Validation
- Yii - AJAX Validation
- Yii - Sessions
- Yii - Using Flash Data
- Yii - Cookies
- Yii - Using Cookies
- Yii - Files Upload
- Yii - Formatting
- Yii - Pagination
- Yii - Sorting
- Yii - Properties
- Yii - Data Providers
- Yii - Data Widgets
- Yii - ListView Widget
- Yii - GridView Widget
- Yii - Events
- Yii - Creating Event
- Yii - Behaviors
- Yii - Creating a Behavior
- Yii - Configurations
- Yii - Dependency Injection
- Yii - Database Access
- Yii - Data Access Objects
- Yii - Query Builder
- Yii - Active Record
- Yii - Database Migration
- Yii - Theming
- Yii - RESTful APIs
- Yii - RESTful APIs in Action
- Yii - Fields
- Yii - Testing
- Yii - Caching
- Yii - Fragment Caching
- Yii - Aliases
- Yii - Logging
- Yii - Error Handling
- Yii - Authentication
- Yii - Authorization
- Yii - Localization
- Yii - Gii
- Gii – Creating a Model
- Gii – Generating Controller
- Gii – Generating Module
- Yii Useful Resources
- Yii - Quick Guide
- Yii - Useful Resources
- Yii - Discussion
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 −
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.