@52fhy
        
        2016-04-28T03:51:01.000000Z
        字数 15447
        阅读 570
    PHP
Yii 是一个高性能的,适用于开发 WEB2.0 应用的 PHP 框架。
Yii目前有两个主要的版本: 2.0 和 1.1。本文以YII 2.0.7为例。
Yii2.0 框架有一些系统上的需求:
PHP 版本 >= 5.4
Mcrypt PHP 扩展
OpenSSL PHP 扩展
Mbstring PHP 扩展
Yii可以通过Composer或者压缩包安装。
与安装Laravel框架类似,很多人会卡在这一步。要么是不会Composer,要么就是无法使用Composer安装。
如果还不知道Composer,先去学习了解了解: 
http://www.cnblogs.com/52fhy/p/5246013.html
如果无法使用用Composer安装,很有可能是仓库访问不了,即被墙了。先更新Composer仓库源:
# 修改 composer 的全局配置文件(推荐方式)# 打开命令行窗口(windows用户)或控制台(Linux、Mac 用户)并执行如下命令:composer config -g repo.packagist composer https://packagist.phpcomposer.com
接下来正式开始安装吧! 
这里以YII 2.0 版本为例(PHP 版本 >= 5.4)。
# 指定2.0.7版本,项目主目录是myyii2composer create-project yiisoft/yii2-app-basic yii2 2.0.7# 或者安装高级的应用程序模板:composer create-project yiisoft/yii2-app-advanced yii2adv 2.0.7
yii2的高级版和基本版的区别在哪里呢?
其实没太大区别。yii2-app-basic只有一个web应用,而yii2-app-advanced是默认带了前台和后台两个web应用,将两个应用整合在一个project里面,然后用common应用存放一些两个应用公共的一些东西。
https://www.zhihu.com/question/27916823
这里以安装yii2-app-basic为例。
等待安装(过程会比较长,期间还要github的token,建议还是下载一键安装包吧):
Installing yiisoft/yii2-app-basic (2.0.7)- Installing yiisoft/yii2-app-basic (2.0.7)Downloading: 100%Created project in yii2Loading composer repositories with package information...Token (hidden):Token stored successfully.- Installing yiisoft/yii2-composer (2.0.3)Downloading: 100%- Installing ezyang/htmlpurifier (v4.6.0)Downloading: 100%- Installing cebe/markdown (1.1.0)Downloading: 100%- Installing bower-asset/jquery (2.1.4)Downloading: 100%- Installing bower-asset/jquery.inputmask (3.1.63)Downloading: 100%- Installing bower-asset/punycode (v1.3.2)Downloading: 100%- Installing bower-asset/yii2-pjax (v2.0.4)Downloading: 100%- Installing yiisoft/yii2 (2.0.4)Downloading: 100%- Installing swiftmailer/swiftmailer (v5.4.1)Downloading: 100%- Installing yiisoft/yii2-swiftmailer (2.0.4)Downloading: 100%- Installing yiisoft/yii2-codeception (2.0.4)Downloading: 100%- Installing bower-asset/bootstrap (v3.3.5)Downloading: 100%- Installing yiisoft/yii2-bootstrap (2.0.4)Downloading: 100%- Installing yiisoft/yii2-debug (2.0.4)Downloading: 100%- Installing bower-asset/typeahead.js (v0.10.5)Downloading: 100%- Installing phpspec/php-diff (v1.0.2)Downloading: 100%- Installing yiisoft/yii2-gii (2.0.4)Downloading: 100%- Installing fzaninotto/faker (v1.5.0)Downloading: 100%- Installing yiisoft/yii2-faker (2.0.3)Downloading: 100%fzaninotto/faker suggests installing ext-intl (*)Writing lock fileGenerating autoload fileschmod('runtime', 0777)...done.chmod('web/assets', 0777)...done.chmod('yii', 0755)...done....
安装过程中注意的:
1、提前安装好Composer2、安装插件:php composer.phar global require "fxp/composer-asset-plugin:1.0.0"3、通过命令行开始安装:这一步耗时比较长,因为它在下载yii2框架。需要github账号,若提示安装过程中输入你的GitHub的用户名和密码。那就输入它们并继续即可。如果需要输入token,注册登录github之后,在setting->Personal access tokens这里生成一个token,然后输入就可以了。复制粘贴(隐藏的),确认。
创建成功会在指定的项目根目录,如yii2目录下生成web\文件夹,里面有入口文件index.php。
安装完便可以访问了:
http://localhost/yii2/web/index.php
显示:
Congratulations!You have successfully created your Yii-powered application.
一键安装包是在 YII 源码的基础上安装了依赖库(也就是已经做过 composer install,已经有了 vendor 目录),对于刚接触 YII2 或者使用 Composer 受挫的用户能够提供快速上手实践 YII2 的捷径。
下载以下归档文件之一,然后将其解压缩到一个Web可访问的文件夹:
yii2-app-basic 
https://github.com/yiisoft/yii2/releases/download/2.0.7/yii-basic-app-2.0.7.tgz
yii2-app-advanced 
https://github.com/yiisoft/yii2/releases/download/2.0.7/yii-advanced-app-2.0.7.tgz
下载很慢的话直接去我的百度云下载: 
http://pan.baidu.com/s/1gfEsVar
注意: 
通过安装包安装的需要在配置文件config/web.php,给 cookieValidationKey 配置项输入你的密钥:
'request' => [// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation'cookieValidationKey' => 'test', //建议填github上的token,随便填也行,不能是空],
cookieValidationKey的作用暂时不做详细讲解。
解压yii-basic-app-2.0.7.tgz到服务器www目录,并更名为yii2。修改config/web.php填写密钥,然后访问:
http://localhost/yii2/web/index.php
提示:
Congratulations!You have successfully created your Yii-powered application.
ok了!
下文均以yii-basic-app-2.0.7.tgz源码进行搭建demo讲解。
yii-basic-app
assets/ assets资源定义commands/ 包含控制台命令类HelloController.phpconfig/ 应用配置db.php 数据库配置params.php 通用参数配置web.php web配置console.php cli配置controllers/ 控制器SiteController.phpmail/ e-mails的视图文件models/ 模型User.phpruntime/ 包含 Yii 在运行时生成的文件,例如日志和缓存文件tests/ contains various tests for the basic applicationvendor/ 包含已经安装的 Composer 包,包括 Yii 框架自身views/ 视图web/ 入口文件和静态资源assets/ 包含 Yii 发布的资源文件(javascript 和 css)index.php 应用入口文件composer.json Composer 配置文件, 描述包信息yii Yii 控制台命令执行脚本yii.bat Yii 控制台命令执行脚本(Windows)
关于yii-basic-app说明: 
在浏览器底部可以看到一个工具栏。这是 Yii 提供的很有用的调试工具,可以记录并显示大量的调试信息,例如日志信息,响应状态,数据库查询等等。
Yii 实现了模型-视图-控制器 (MVC)设计模式,这点在上述目录结构中也得以体现。 models 目录包含了所有模型类,views 目录包含了所有视图脚本,controllers 目录包含了所有控制器类。
Yii 流程: 
1. 用户向入口脚本 web/index.php 发起请求。 
2. 入口脚本加载应用配置并创建一个应用实例去处理请求。 
3. 应用通过请求组件解析请求的路由。 
4. 应用创建一个控制器实例去处理请求。 
5. 控制器创建一个操作实例并针对操作执行过滤器。 
6. 如果任何一个过滤器返回失败,则操作退出。 
7. 如果所有过滤器都通过,操作将被执行。 
8. 操作会加载一个数据模型,或许是来自数据库。 
9. 操作会渲染一个视图,把数据模型提供给它。 
10. 渲染结果返回给响应组件。 
11. 响应组件发送渲染结果给用户浏览器。
控制器路径:controllers。使用 app\controllers 命名空间。
示例:TestController.php
<?phpnamespace app\controllers;use Yii;use yii\web\Controller;class TestController extends Controller{/*** index* Yii 使用 action 前缀区分普通方法和操作。action 前缀后面的名称被映射为操作的 ID*/public function actionIndex($msg = 'hello') {//index 视图应该存为 views/test/index.php 文件//按 views/控制器 ID/视图名.php 路径加载 PHP 文件return $this->render('index', array('msg' => $msg));}/*** my-index* 操作 ID 总是被以小写处理,如果一个操作 ID 由多个单词组成,单词之间将由连字符连接(如 my-index);* 操作 ID 映射为方法名时移除了连字符,将每个单词首字母大写,并加上 action 前缀。例子:操作 ID my-index 相当于方法名 actionMyIndex*/public function actionMyIndex($msg = 'hello') {return $this->render('my-index', array('msg' => $msg));}}
创建对应的视图: 
views/test/index.php 
views/test/my-index.php
<!DOCTYPE html><head><meta charset="utf-8"><title>test</title></head><body><?php echo $msg;?></body>
访问:
http://localhost/yii2/web/index.php?r=test/index&msg=hellohttp://localhost/yii2/web/index.php?r=test/my-index&msg=welcome
Yii::$app->urlManager->createUrl('index/test');
结果:/yii2/web/index.php?r=index%2Ftest
\yii\helpers\Url::to(array('index/test'));
结果:/yii2/web/index.php?r=index%2Ftest
YII 路由与大多数框架如ThinkPHP、CI类似,是自动化的,不需要单独去配置。这点与Laravel不同。
URL 中的参数 r 代表路由。路由格式是 控制器 ID/操作 ID。
示例:
http://localhost/yii2/web/index.php?r=test/index&msg=hello
代表TestController/actionIndex,其中方法actionIndex接收了一个参数,键名是msg,键值是hello。
实际上我们使用$_GET也可以获取URL上的所有参数,包括路由r。
//开启debug,应用会保留更多日志信息,如果抛出异常,会显示详细的错误调用堆栈defined('YII_DEBUG') or define('YII_DEBUG', true);//环境定义defined('YII_ENV') or define('YII_ENV', 'dev'); //dev,prod
包含: 
db.php       数据库配置 
params.php   通用参数配置 
web.php      web配置 
console.php  cli配置
详见下章:http://www.cnblogs.com/52fhy/p/5349394.html
Yii2 模型文件都放在 models 目录下,以及默认使用 app\models 命名空间。
Yii 里模型的类名与文件名是一模一样的。例如UserAddress.php对应类UserAddress。建议使用驼峰命名。
示例:models/EntryForm.php
<?phpnamespace app\models;use yii\base\Model;class EntryForm extends Model{public $name;public $email;/*** 返回数据验证规则的集合*/public function rules(){return [//name 和 email 值都是必须的[['name', 'email'], 'required'],//email 的值必须满足email规则验证['email', 'email'],];}}
注意:yii\base\Model 被用于普通模型类的父类并但与数据表无关。真正与数据的CURD相关的是yii\db\ActiveRecord,继承自 yii\base\Model,增加了数据库处理。
上面的示例中验证规则(rules)可以在控制器使用 yii\base\Model::validate() 方法触发数据验证。
需要引入命名空间并实例化模型:
<?phpnamespace app\controllers;use Yii;use yii\web\Controller;use app\models\EntryForm;//引入命名空间class TestController extends Controller{public function actionIndex($msg = 'hello') {$model = new EntryForm();if ($model->load(Yii::$app->request->post()) && $model->validate()) {return $this->render('index', array('msg' => $msg));}}}
表达式
Yii::$app代表应用实例,它是一个全局可访问的单例。同时它也是一个服务定位器,能提供 request,response,db 等等特定功能的组件。在上面的代码里就是使用 request 组件来访问应用实例收到的$_POST数据。
视图根路径:views。
视图与控制器对应关系: 
按 views/控制器 ID/视图名.php 路径加载 PHP 文件
以上面的TestController为例,有:
$this->render('index', array('msg' => $msg));$this->render('my-index', array('msg' => $msg));
分别对应:
views/test/index.phpviews/test/my-index.php
即控制器/方法与视图目录是一一对应的。
视图内容其实就是html代码。为了防止跨站脚本(XSS)攻击,我们可以在视图中使用yii\helpers\Html::encode()进行处理:
views/test/index.php
<!DOCTYPE html><head><meta charset="utf-8"><title>test</title></head><body><?php use yii\helpers\Html; ?><?= Html::encode($msg) ?></body>
视图可以有子目录:
$this->render('sub/index', array('msg' => $msg));
对应:
views/test/sub/index.php
$this->render('index', array('msg' => $msg));
数据库相关配置文件都在 config/db.php:
<?phpreturn ['class' => 'yii\db\Connection','dsn' => 'mysql:host=localhost;dbname=yii2basic','username' => 'root','password' => '','charset' => 'utf8',];
上面配置的数据库连接可以在应用中通过 Yii::$app->db 表达式访问。
为了方便下文继续演示,这里新建一个数据库yii2basic,并新建表country:
CREATE TABLE `country` (`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,`code` CHAR(2) NOT NULL ,`name` CHAR(52) NOT NULL,`population` INT(11) NOT NULL DEFAULT '0') ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `country` VALUES (null, 'AU','Australia',18886000);INSERT INTO `country` VALUES (null, 'CA','Canada',1147000);INSERT INTO `country` VALUES (null, 'CN','China',1277558000);INSERT INTO `country` VALUES (null, 'US','United States',278357000);
为了使用ActiveRecord里面的方法,这里不再继承\yii\base\Model,而是继承\yii\db\ActiveRecord。
这里建立一个模型Country:
<?phpnamespace app\models;use yii\db\ActiveRecord;class Country extends ActiveRecord{}
在里面写任何代码。只需要像现在这样,Yii就能根据类名去猜测对应的数据表名。如果类名和数据表名不能直接对应,可以覆写yii\db\ActiveRecord::tableName() 方法去显式指定相关表名:
public static function tableName() {return 'country';}
控制器里使用模型: 
首先引入命名空间:
use app\models\Country;
然后直接使用模型里的数据库操作方法:
public function actionListCountry() {//获取所有$list = Country::find()->orderBy('name')->all();//获取一条$one = Country::findOne('US');print_r($one->name);//更新/ 修改 name 为 U.S.A. 并在数据库中保存更改$one->name = 'U.S.A.';$one->save();}
\yii\db\ActiveQuery继承自\yii\db\Query。
通过\yii\db\ActiveRecord::find()可获取\yii\db\ActiveQuery实例。
下面以Customer模型及customer作为演示。
yii\db\ActiveRecord::save()yii\db\ActiveRecord::insert()
示例:
$customer = new Customer();$customer->name = 'Qiang';$customer->save(); // 等同于 $customer->insert();
yii\db\ActiveRecord::delete()//静态方法,作用于整张表yii\db\ActiveRecord::deleteAll()
示例:
// 删除已有客户记录$customer = Customer::findOne($id);$customer->delete();// 删除多个年龄大于20,性别为男(Male)的客户记录Customer::deleteAll('age > :age AND gender = :gender', [':age' => 20, ':gender' => 'M']);
yii\db\ActiveRecord::save()yii\db\ActiveRecord::update()//静态方法,作用于整张表yii\db\ActiveRecord::updateCounters()yii\db\ActiveRecord::updateAll()yii\db\ActiveRecord::updateAllCounters()
示例:
$customer = Country::findOne('Li');$customer->name = 'Qiang';$customer->save(); // 等同于 $customer->update();// 所有客户的age(年龄)字段加1:Customer::updateAllCounters(['age' => 1]);
yii\db\ActiveRecord::find() //返回ActiveRecord对象yii\db\ActiveRecord::findBySql() //通过SQL查询返回ActiveRecord对象// 两个快捷方法:直接返回一个或者一组结果yii\db\ActiveRecord::findOne() //返回第一个匹配到的实例yii\db\ActiveRecord::findAll() //返回所有
find()返回一个ActiveRecord对象,需要使用one()或者all()获取最终数据。 
findBySql()接收SQL,返回一个ActiveRecord对象,需要使用one()或者all()获取最终数据。
findOne()和findAll()直接返回对象数组,不需要再使用one()或者all()。
都支持静态方法或者直接调用。
示例:
/**find**/$m = new Country();$m->find()->all();//返回对象$m->find()->one();//返回对象,仅返回一条记录$m->find()->asArray()->all();//asArray()返回数组$m->find()->select('name')->column(); //返回指定字段的数组集合$m->find()->column();//返回第一个字段的数组集合$m->find()->where(['name'=> 'China'] )->one();$m->find()->where(['name'=> 'China'] )->all();$m->find()->where(['name' => 'China', 'code' => 'CN'] )->all();$m->find()->where(['name'=> 'China'] )->offset(1)->limit(1)->all();$m->find()->orderBy('id DESC')->all();$m->find()->where(['name'=> 'China'] )->orderBy('code desc')->limit(2)->all();//andWhere与where是一样的,多个建议使用andWhere、orWhere$m->find()->where(['id' => 1])->count();$m->find()->where(['id' => 1])->andWhere(['code' => 'CN'])->count();$m->find()->where(['id' => 1])->orWhere(['code' => 'CN'])->count();//findBySql$m->findBySql('select * from country')->one();$m->findBySql('select * from country')->all();/* where高级用法:当参数大于2个时且无键值对,第一个表示操作符* [操作符, 操作数1, 操作数2, ...]*///比较:>,<,=,>=,<=$m->find()->where(['=', 'id', '3'])->all(); //id=3$m->find()->where(['>', 'id', '3'])->all(); //id>=3//逻辑:and,or$m->find()->where(['and', 'id=5', ['code' => 'CN']])->all();//即 id=5 AND code ='CN';字符型操作数需要用数组表示$m->find()->where(['or', 'id=5', 'id=1'])->all();//between,not between$m->find()->where(['between', 'id', 1, 3])->all(); //id between 1 AND 3//in,not in$m->find()->where(['in', 'id', [1,2,3]])->all(); //id in (1,2,3)//like,or like, not like,or not like 不支持左右like$m->find()->where(['like', 'name', 'A'])->all();$m->find()->where(['like', 'name', ['A','B']])->all();//exists,not exists//其它$m->find()->scalar(); //此方法返回值的第一行第一列的值$m->find()->count(); //此方法返回记录的数量$m->find()->average(); //此方法返回指定列的平均值$m->find()->min(); //此方法返回指定列的最小值$m->find()->max(); //此方法返回指定列的最大值$m->find()->exists();//此方法返回一个bool值指示是否可以查询数据//返回数组以 ID 为索引$m->find()->indexBy('id')->all();//distinct() 方法来去除重复行$m->find()->select('name')->distinct()->all(); //SELECT DISTINCT `name` FROM `country`//groupBy,having//SELECT `name`, count(*) as sum FROM `country` GROUP BY `name`$m->find()->select(['name','count(*) as sum'])->groupBy('name')->all();//SELECT `name`, count(*) as sum FROM `country` GROUP BY `name` HAVING `sum` >= 2$m->find()->select(['name','count(*) as sum'])->groupBy('name')->having(['>=','sum', 2])->all();//多表关联建议使用SQL// yii\db\Query::join($type, $table, $on, $params) 类型(join,left,right),需要join的表,连接条件,可选参数$m->find()->join('inner','articles', 'country.id = articles.id')->all();$m->find()->innerJoin('articles', 'country.id = articles.id')->all();//SELECT * FROM `country` INNER JOIN `articles` ON country.id = articles.id/**findOne(),findAll()**/Country::findOne(1); // 返回 id 为 1 的数据Country::findOne(['id' => 1,'status' => 1,]);Country::findAll([1, 2, 3]); // 返回id为1、2、3的一组数据Country::findAll(['status' => 1,]);/**批量获取**/// 一次提取 10 个客户信息foreach ($m->find()->batch(10) as $customers) {// $customers 是 10 个或更少的客户对象的数组}// 一次提取 10 个客户并一个一个地遍历处理foreach ($m->find()->each(10) as $customer) {// $customer 是一个 ”Customer“ 对象}// 贪婪加载模式的批处理查询foreach ($m->find()->with('orders')->each() as $customer) {}
注意: 
调用 save()、insert()、update() 这三个方法时,会自动调用yii\base\Model::validate()方法。如果验证失败,数据将不会保存进数据库。
更多查看:http://www.yiichina.com/doc/guide/2.0/db-active-record
由于ActiveRecord是通过\yii\db\ActiveRecord::find()获取的\yii\db\ActiveQuery实例,而\yii\db\ActiveQuery继承自\yii\db\Query,那么其实可以直接使用\yii\db\Query里的方法进行查询数据。
此时,除了ActiveRecord里的find()、insert()、update()、updateAll、delete()、deleteAll()、findBySql()、asArray()等不能使用外,其它的都是可以用的,例如where()、all、one、limit等等。
可以这么说,对于ActiveRecord里示例里的查询方法,只要把find()替换成from($table)就可以用了。Query默认返回的数组,不是对象。下面是例子:
$m = new \yii\db\Query();$table = 'country';$m->from($table)->all();//返回数组$m->from($table)->one();//返回数组,仅返回一条记录$m->from($table)->select('name')->column(); //返回指定字段的数组集合$m->from($table)->column();//返回第一个字段的数组集合$m->from($table)->where(['name'=> 'China'] )->one();$m->from($table)->where(['name'=> 'China'] )->all();$m->from($table)->where(['name' => 'China', 'code' => 'CN'] )->all();$m->from($table)->where(['name'=> 'China'] )->offset(1)->limit(1)->all();$m->from($table)->orderBy('id DESC')->all();$m->from($table)->where(['name'=> 'China'] )->orderBy('code desc')->limit(2)->all();//andWhere与where是一样的,多个建议使用andWhere、orWhere$m->from($table)->where(['id' => 1])->count();$m->from($table)->where(['id' => 1])->andWhere(['code' => 'CN'])->count();$m->from($table)->where(['id' => 1])->orWhere(['code' => 'CN'])->count();
http://www.yiichina.com/doc/guide/2.0/db-query-builder
在Yii中,使用 yii\db\Transaction 来表示数据库事务。
一般情况下,我们从数据库连接启用事务,通常采用如下的形式:
$connection = \Yii::$app->db;$transaction = $connection->beginTransaction();try {$connection->createCommand($sql1)->execute();$connection->createCommand($sql2)->execute();// ... 执行其他 SQL 语句 ...$transaction->commit();} catch(Exception $e) {$transaction->rollBack();}
示例:
$dbTrans= Yii::app()->db->beginTransaction();try{$customer = new Customer();$customer->name = 'Qiang';if(!$customer->save()) throw newException("Error Processing Request", 1);$dbTrans->commit();// $this->_end(0,'添加成功!!!');}catch(Exception$e){$dbTrans->rollback();// $this->_end($e->getCode(),$e->getMessage());
深入理解: 
http://www.yiichina.com/doc/guide/2.0/db-dao 
http://www.digpage.com/transaction.html 
http://blog.csdn.net/xundh/article/details/45955195/
Yii 2.0 权威指南 - 文档 - Yii Framework 中文社区 
http://www.yiichina.com/doc/guide/2.0