Home > PHP Framework > YII > body text

What are the differences between yii1 and yii2?

青灯夜游
Release: 2019-12-23 17:43:13
Original
4200 people have browsed it

Yii is a high-performance PHP5 web application development framework. A simple command line tool yiic can quickly create a web application code framework. Developers can add business logic based on the generated code framework to quickly complete application development.

What are the differences between yii1 and yii2?

The Yii2.0 version framework is completely rewritten, and there are quite a few differences between the 1.1 and 2.0 versions. [Recommended learning: Yii Getting Started Tutorial]

So what are the differences between yii1 and yii2?

Yii 2.0 requires PHP 5.4 or higher, which is a huge improvement over the PHP 5.2 required by Yii 1.1.

1. The application instance directly uses global named variables to access: $app without calling app().

2. A significant change in the view layer of Yii2 is the introduction of view classes, which makes the implementation of the MVC pattern more complete. Correspondingly, the relevant presentation layer auxiliary classes are managed by the new view class. For example, theme: Yii::app()->theme->baseUrl should be updated to Yii::$app->view-> theme->baseUrl, or $this->theme->baseUrl.

3. Yii2 introduces the concept of resource packages, and the way of resource reference has changed greatly.

There are two ways to introduce resources, one is through the AppAsset class in the assets directory, and the other is through a registration method similar to Yii1.

Yii::app()->getClientScript() method is no longer available. For example, if you want to dynamically register a JS script file in the page, the changes are as follows:

Yii::app() ->getClientScript()->registerScriptFile('...') is changed to Yii::$app->view->registerJsFile('...'),

or $this- >registerJsFile('...').

4. Use the database

For example, query a record with user_id

yii1:

User::model()->find(' user_id=:user_id',[':user_id'=>$user_id]);

yii2:

User::find()->where('user_id=:user_id' ,[':user_id'=>$user_id])->one();

Query multiple records:

yii1:

User::model( )->findAll('status=:status',[':staus'=>$status]);

yii2:

User::find()->where ('status=:status',[':staus'=>$status])->all();

In addition, yii2 also provides the asArray() method, and the direct query result is an array:

User::find()->where('status=:status',[':staus'=>$status])->asArray()->all();

If you are a beginner, it is recommended to learn yii2 directly.

The above is the detailed content of What are the differences between yii1 and yii2?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
yii
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template