The entry script is the first step in the application startup process. An application (whether it is a web application or a console application) has only one entry script. End-user requests instantiate the application through the entry script and forward the request to the application.
The entry script of the Web application must be placed in a directory that can be accessed by end users. It is usually named index.php. You can also use the Web server to Other names targeted.
The entry script of the console application is generally named yii (suffix .php) in the application root directory. The file requires execution permission so that the user can pass the command ./yii
The entry script mainly completes the following work:
Define global constants;
Register Composer autoloader;
Contains Yii class files;
Load application configuration;
Create an application instance and configure it;
Call yii\ base\Application::run() to handle the request.
Web Application
The following is the code of the basic application template entry script:
<?php defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); // 注册 Composer 自动加载器 require __DIR__ . '/../vendor/autoload.php'; // 包含 Yii 类文件 require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php'; // 加载应用配置 $config = require __DIR__ . '/../config/web.php'; // 创建、配置、运行一个应用 (new yii\web\Application($config))->run();
Console Application
The following is the entry script for a 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/ */ defined('YII_DEBUG') or define('YII_DEBUG', true); defined('YII_ENV') or define('YII_ENV', 'dev'); // 注册 Composer 自动加载器 require __DIR__ . '/vendor/autoload.php'; // 包含 Yii 类文件 require __DIR__ . '/vendor/yiisoft/yii2/Yii.php'; // 加载应用配置 $config = require __DIR__ . '/config/console.php'; $application = new yii\console\Application($config); $exitCode = $application->run(); exit($exitCode);
Define constants
Entry script It is the best place to define global constants. Yii supports the following three constants:
YII_DEBUG: identifies whether the application is running in debug mode. When in debug mode, the application will retain more log information, and if an exception is thrown, a detailed error call stack will be displayed. Therefore, debug mode is mainly suitable for use during the development phase, and the default value of YII_DEBUG is false.
YII_ENV: Identifies the environment in which the application runs. Please refer to the configuration chapter for details. The default value of YII_ENV is 'prod', which means the application runs in an online production environment.
YII_ENABLE_ERROR_HANDLER: Identifies whether to enable error handling provided by Yii. The default is true.
When defining a constant, it is usually defined using code similar to the following:
defined('YII_DEBUG') or define('YII_DEBUG', true);
The above code is equivalent to:
if (!defined('YII_DEBUG')) { define('YII_DEBUG', true); }
Obviously the first paragraph The code is simpler and easier to understand.
Constant definitions should be at the beginning of the entry script, so that when other PHP files are included, the constants will take effect.
The above is the detailed content of What is the use of the yii framework entry script?. For more information, please follow other related articles on the PHP Chinese website!

Yii remains a powerful choice for developers. 1) Yii is a high-performance PHP framework based on the MVC architecture and provides tools such as ActiveRecord, Gii and cache systems. 2) Its advantages include efficiency and flexibility, but the learning curve is steep and community support is relatively limited. 3) Suitable for projects that require high performance and flexibility, but consider the team technology stack and learning costs.

Yii framework is suitable for enterprise-level applications, small and medium-sized projects and individual projects. 1) In enterprise-level applications, Yii's high performance and scalability make it outstanding in large-scale projects such as e-commerce platforms. 2) In small and medium-sized projects, Yii's Gii tool helps quickly build prototypes and MVPs. 3) In personal and open source projects, Yii's lightweight features make it suitable for small websites and blogs.

The Yii framework is suitable for building efficient, secure and scalable web applications. 1) Yii is based on the MVC architecture and provides component design and security features. 2) It supports basic CRUD operations and advanced RESTfulAPI development. 3) Provide debugging skills such as logging and debugging toolbar. 4) It is recommended to use cache and lazy loading for performance optimization.

Yii's purpose is to enable developers to quickly and efficiently build web applications. Its implementation is implemented through the following methods: 1) Component-based design and MVC architecture to improve code maintainability and reusability; 2) Gii tools automatically generate code to improve development speed; 3) Lazy loading and caching mechanism optimization performance; 4) Flexible scalability to facilitate integration of third-party libraries; 5) Provide RBAC functions to handle complex business logic.

Yiiisversatileavssuitable Projectsofallsizes.1) Simple Sites, YiiOofferseassetupandrapiddevelopment.2) ForcomplexProjects, ITModularityandrbacSystemManagescalabilityandSecurity effective.

The Yii framework will continue to play an important role in the future development of PHP frameworks. 1) Yii provides efficient MVC architecture, powerful ORM system, built-in caching mechanism and rich extension libraries. 2) Its componentized design and flexibility make it suitable for complex business logic and RESTful API development. 3) Yii is constantly updated to adapt to modern PHP features and technical trends, such as microservices and containerization.

The Yii framework is suitable for developing web applications of all sizes, and its advantages lie in its high performance and rich feature set. 1) Yii adopts an MVC architecture, and its core components include ActiveRecord, Widget and Gii tools. 2) Through the request processing process, Yii efficiently handles HTTP requests. 3) Basic usage shows a simple example of creating controllers and views. 4) Advanced usage demonstrates the flexibility of database operations through ActiveRecord. 5) Debugging skills include using the debug toolbar and logging system. 6) Performance optimization It is recommended to use cache and database query optimization, follow coding specifications and dependency injection to improve code quality.

In Yii2, there are two main ways to display error prompts. One is to use Yii::$app->errorHandler->exception() to automatically catch and display errors when an exception occurs. The other is to use $this->addError(), which displays an error when model validation fails and can be accessed in the view through $model->getErrors(). In the view, you can use if ($errors = $model->getErrors())


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor
