Home >php教程 >php手册 >Yii2 first experience

Yii2 first experience

WBOY
WBOYOriginal
2016-10-19 10:19:341237browse

Seeing that Yii1.1 has so many dissatisfaction, and seeing Yii2 maturing day by day, I put together a prototype stage of a small project and tried to use Yii2.

I wrote down some experience, briefly speaking from the perspective of a Yii1 skilled worker looking at Yii2. (Additions may be added at the end of this article at any time in the future)

  • The default template uses bootstrap, which is very beautiful.
  • The config files of the basic version and the advanced version are different. In the basic version, there is web.php, but the latter does not.
  • Set to display in Chinese: In config/web.php, add a line 'language'=>'zh-CN'. Then Home became Homepage, and Powered by Yii Framework became Technical Support Yii Framework.
  • Use gii to generate the model. I checked Use Table Prefix. The tableName() function of the generated model returns {{%menu}}. If not checked, the full name of the table name will be returned, such as ssn_menu
  • Many functions have been added to models/user.php, which is much more convenient.
  • In models and controllers, use [ ... ] instead of array(...) everywhere, which is much more beautiful and refreshing.
  • The corresponding URL of LibCrudController is ?r=lib-crud
  • Start trying to customize the user login part. . http://www.yiichina.com/tutorial/332
  • Start doing CRUD (from 20:20)
  • User::find()->all() instead of User::model()->findAll()
  • To reference User in _form.php, you need to declare use appmodelsUser;
  • first
  • To reference ArrayHelper in the view file, you must first declare use yiihelpersArrayHelper;
  • In Yii v1.1, the data entry element requires three lines, but here it only needs one line:
field($model, 'name1')->textInput(['maxlength' => true]) ?>
  • The CRUD interface generated by gii is much more elegant and beautiful, and the prompt information is automatically in Chinese (this should be the credit of GridView)
  • User::findOne($user_id); is different from the one below. This returns a record, I don’t know what the following is.
$user=User::find()->where(['id' => $id]);
  • Add my.css: First add my.css in web/css, and then in assets/AppAsset.php
public $css = [
'css/site.css',
'css/my.css',
];
  • Insert js file: $this->registerJsFile("js/start.js"); --When relying on jquery, this obviously does not work, and it will prompt that $ cannot be found.
  • https://segmentfault.com/a/1190000003742452 It’s very clear here. Several options. The plan to modify assets/AppAsset.php has been adjusted. One of the key points is (see boldface): AppAsset::addScript($this,'@web/js/start.js');
  • There is a cool debug-toolbar button on the lower right side of the page. Clicking it will pop up a horizontal status bar.
  • index.php?r=site/index in the address bar will become index.php?r=site%2Findex
Statement:
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
Previous article:substr() function in phpNext article:substr() function in php