(一)下载
点击 基本应用模板 会在github 上下载最新的 目前是 2.0.9 ,由于公司用的是2.0.6 我是在csdn 下的
(二)测试是否安装成功
解压后会自动生成 basic的文件夹
http://localhost/basic/requirements.php 打开这个文件 检测当前环境时候能运行yii
这样就可以了
出现notice 错误 可以在php.ini中屏蔽点
打开入口文件 出现错误
'cookieValidationKey' => 'aa', 随意填个值就行
出现这个就说明成功了
(三)控制器的创建
控制器的目录
控制器写法 命名空间要写 use 前面 不然会报错
浏览器访问
(四)控制器请求组件 (接收get post 值)
1 接收get 值
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
class HelloController extends Controller{
public function actionIndex(){
$request = \YII::$app->request; //调用 YII 里面的request组件 YII是全局类 所以要加反斜杠
echo $request->get('id');
}
}
2 接收get 值 但值为空
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
class HelloController extends Controller{
public function actionIndex(){
$request = \YII::$app->request; //调用 YII 里面的request组件 YII是全局类 所以要加反斜杠
echo $request->get('id',20);//第二个参数 若 get 值不存在就输出 20
}
}
3 接收post值
$request = \YII::$app->request; //调用 YII 里面的request组件 YII是全局类 所以要加反斜杠
echo $request->post('id',20);//第二个参数 若 post 值不存在就输出 20
4 判断 get 还是post
$request
= \YII::$app->request;
if($request->isGet){
echo 'this is get ';
}
判断post
if($request->isPost){
echo 'this is post ';
}
5 得到用户IP
$request
= \YII::$app->request;
echo $request->userIp;
(五)控制器相应处理
1
响应组件获取 设置状态码
获取相应组件将 状态吗 设置成404
2
添加header头 pragma参数设置
header头 pragma参数 变成no-cahce
2
设置header头
pragma参数设置
$res->headers->set('pragma','max-age-5'); //设置 header头 pragma参数
3
删除header头
pragma参数设置
$res->headers->remove('pragma'); //删除header头 pragma参数
4
跳转 两种方法都行
$this->redirect('http://www.baidu.com');
5
文件下载 两种方法都行
把
访问的页面 下载下来 命名为 a.jpg
$res ->headers->add('content-disposition','attachment;filename="a.jpg"');
把 web 根目录 的robots.txt文件 下载下来
$res->sendFile('./robots.txt');
(五)session处理
1 调用session
$session=\YII::$app->session; //
调用session组件
$session->open(); //
打开session
if($session->isActive){ //
判断session是否生效
echo 'session is work';
}
把session 当成一个对象
$session->set('name','张三'); //设置session
echo $session->get('name'); //得到session值
$session->remove('name'); //删除session
也可以把session 当成一个数组
$session['name']='张三';
//设置session
unset($session['name']);
//删除session
session 的信息 其实存到一个文件中 通过 php.ini 可以知道 找到存放session的文件名
(六) cookie处理
use yii\web\Cookie
打开调试工具 就写入了一条cookie
删除cookie
$cookies->remove(’user‘);
得到 cookie值
$cookies=\YII::$app->response->cookies; //得到cookie
$cookie_data=array('name'=>'user','value'=>'sss');
$cookies->add(new Cookie($cookie_data)); //
将数据存入cookie;
$cookie = \YII::$app->request->cookies;
echo $cookie->getValue('user');
得到 sss
如果 getValue 参数 不存在
echo $cookie->getValue('userddd','gg');
得到 gg
控制台中 cookie 的value 值 是一串加密的字符串
'cookieValidationKey' => 'aa', 这里设置的加密方式 自己设置的
(七) 视图处理
1 视图的创建
模板文件 文件夹以控制名一样 文件与方法名一样 模板文件的后缀也是php
2 数据的传递
(1)字符串类型
控制器
$str='hello hello';
$data['aaa']=$str;
return $this->renderPartial('index',$data);
模板
<h1><?=$aaa;?></h1>
(2)数组类型
控制器
$arr=array('a'=>'aaaaaaa','b'=>'bbbbbbbbbbbbb');
$data['bbb']=$arr;
return $this->renderPartial('index',$data);
模板
<h1><?=$bbb['b'];?></h1>
3 数据的传递的安全性
(1)
控制器
$str='hello hello<script>alert(2);</script>';
$data['aaa']=$str;
return $this->renderPartial('index',$data);
模板
<h1><?=$aaa;?></h1> 这样就会出现弹框
(2)
<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
?>
<h1><?=Html::encode($aaa);?></h1> // hello hello<script>alert(2);</script>
<h1><?=HtmlPurifier::process($aaa);?></h1> //
hello hello
4 布局文件
控制器
视图文件 index.php
<h1>index </h1>
布局文件 common.php
( 1 )布局文件 在views\layous 下面 使用时要在控制器 中 申明这个变量
(2)控制器中render 函数 一会加载 模板文件 common.php 二会将 index.php的信息 赋值给$content这个变量 所有 在布局 文件中国<?=$content;?> 就会显示 index.php里面的内容
5 一个视图文件包含另外一个视图文件
控制器
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\web\Cookie;
class HelloController extends Controller{
public function actionIndex(){
return $this->renderPartial('index');
}
}
index.php视图
hello world
<?php echo $this->render('new');?>
new.php 视图
new world
结果
hello world new world
new.php
和 index.php 都在 views\hello中
6 一个视图文件包含另外一个视图文件 并且输出变量
控制器
<?php
namespace app\controllers;
use yii\web\Controller;
use yii\web\Cookie;
class HelloController extends Controller{
public function actionIndex(){
return $this->renderPartial('index');
}
}
index.php视图
hello world
<?php echo $this->render('new',array('ddd'=>'old world'));?>
new.php 视图
new world
<?=$ddd;?>
结果
hello world new world old world
new.php
和 index.php 都在 views\hello中
7
视图块(修改布局文件中的内容)
控制器
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
use yii\web\Cookie;
class HelloController extends Controller{
public $layout='common'; //定义公共的引用模板名为common
public function actionIndex(){
// 数据块 就是 修改布局文件中的内容 即修改一对标签里面的内容
return $this->render('index');
}
}
index.php视图
hello world
<?php $this->beginBlock('aaa');?>
<h1>替换布局文件中h1中的内容</h1>
<?php $this->endBlock();?>
布局文件common.php视图
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php if (isset($this->blocks['aaa'])):?>
<?=$this->blocks['aaa'];?>
<?php else:?>
<h1>common文件</h1>
<?php endif;?>
<?=$content;?>
</body>
</html>
blocks是一个数组 要是index.php存在数据模块 就会把 布局文件中h1标签的内容替换 不然就会显示布局文件
h1标签里面的内容
(八)
数据模型
1
数据库配置文件
有dsn说明是
pdo连接数据库
2 单表查询 test 表
首先写一个 test 的模型类
Test.php 内容
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Test extends ActiveRecord{
}
对 yii 数据库 text 进行操作 text的model类 已经 写好 需要 在 index 方法前 use 进来
控制器HelloController.php内容
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
use yii\web\Cookie;
use app\models\Test;//导入text的命名空间
use app\models\Customer;//导入Customer的命名空间
use app\models\Order;//导入Order的命名空间
class HelloController extends Controller{
public function actionIndex(){
//单表查询 sql 语句方式
// $sql='select *from test where id =1';
// $aaa=Test::findBySql($sql)->all(); //
得到是一个数组 数组里面每一个值是一个对象
}
}
3 防止sql注入
控制器HelloController.php内容
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
use yii\web\Cookie;
use app\models\Test;//导入text的命名空间
use app\models\Customer;//导入Customer的命名空间
use app\models\Order;//导入Order的命名空间
class HelloController extends Controller{
public function actionIndex(){
// 用户传的$id 错误方式
// $id=1 or 1=1; 这样 where条件就是一个布尔的真 相当于把所有的数据都查出了来
// $sql='select *from test where id =$id';
// $aaa=Test::findBySql($sql)->all();
// yii里面提供占位符解决这一问题 正确方式 基础知识是pdo的处理
// $id 是用户传的
// $id='1 or 1=1';
// $sql='select *from test where id =:id';
// $aaa=Test::findBySql($sql,array(':id'=>"$id"))->all();
// var_dump($aaa);
}
}
4 数组写法
控制器HelloController.php内容
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
use yii\web\Cookie;
use app\models\Test;//导入text的命名空间
use app\models\Customer;//导入Customer的命名空间
use app\models\Order;//导入Order的命名空间
class HelloController extends Controller{
public function actionIndex(){
//id=1
// $aaa=Test::find()->where(['id'=>1])->all();
//id>0
// $aaa=Test::find()->where(['>','id',0])->all();
//id>=1 并且<=2
// $aaa=Test::find()->where(['between','id',1,2])->all();
// echo count($aaa); //显示数据条数
// title like %title1%
// $aaa=Test::find()->where(['like','title','title1'])->all();
}
}
5 将得到的查询数据 数组里面的每一个对象转换成 数组(对象太占内存)
asArray()
控制器HelloController.php内容
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
use yii\web\Cookie;
use app\models\Test;//导入text的命名空间
use app\models\Customer;//导入Customer的命名空间
use app\models\Order;//导入Order的命名空间
class HelloController extends Controller{
public function actionIndex(){
//当查询条件成千上万是 all() 这个得到的大数组下面是对象 对内存消耗很大
// 1 将 对象转化成数组 asArray() 得到的就是一个二维数组
// $aaa=Test::find()->where(['between','id',1,2])->asArray()->all();
}
}
6 批量查询
控制器HelloController.php内容
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
use yii\web\Cookie;
use app\models\Test;//导入text的命名空间
use app\models\Customer;//导入Customer的命名空间
use app\models\Order;//导入Order的命名空间
class HelloController extends Controller{
public function actionIndex(){
// 2 批量查询
// foreach(Test::find()->batch(1) as $ddd){
// var_dump(count($ddd));
// }
//结果 int(1) int(1) 数据库有两条数据 每次只查1条
}
}
7 数据删除
控制器HelloController.php内容
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
use yii\web\Cookie;
use app\models\Test;//导入text的命名空间
use app\models\Customer;//导入Customer的命名空间
use app\models\Order;//导入Order的命名空间
class HelloController extends Controller{
public function actionIndex(){
//删除
//1 先查出来 再调用对象的方法
// $aaa=Test::find()->where(['id'=>1])->all();
// $aaa[0]->delete();
//就把id等于1 的第一条数据删除掉
//2 调用deldteAll 方法
// Test::deleteAll(); // 把所有数据都删掉了 所以里面要有条件 也可以用占位符
//Test::deleteAll('id>:id',array(':id'=>4)); //
把id 大于4的 都删除了
}
}
Test.php
内容
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Test extends ActiveRecord{
// 添加数据的的验证器
public function rules(){
return[
['id','integer'], //整型的验证器
['title','string','length'=>[0,5]] //
字符串的验证器 并且 长度在 0-5之间
];
}
}
控制器HelloController.php内容
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
use yii\web\Cookie;
use app\models\Test;//导入text的命名空间
use app\models\Customer;//导入Customer的命名空间
use app\models\Order;//导入Order的命名空间
class HelloController extends Controller{
public function actionIndex(){
//添加数据 先在 test 的模型类中写 验证器
// $test=new Test;
// $test->id=1; 测试时是手动添加 实际工作时表单体交
// $test->title='ccc';
// $test->validate(); //调用test模型类中的验证器
// if($test->hasErrors()){ //
调用 判断错误函数
// echo 'error';
// die;
// }
// $test->save(); // 插入数据
}
}
9 修改数据
控制器HelloController.php内容
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
use yii\web\Cookie;
use app\models\Test;//导入text的命名空间
use app\models\Customer;//导入Customer的命名空间
use app\models\Order;//导入Order的命名空间
class HelloController extends Controller{
public function actionIndex(){
//修改数据 修改的数据 test模型类中的 验证器也起作用
// $aaa=Test::find()->where(['id'=>1])->one();
// $aaa->title='bbb';
// $aaa->save();
}
}
10 两表关联查询 customer order 表
Customer.php
内容
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Customer extends ActiveRecord{
public function getOrder(){
//$this 相当于 实例化的customer对象
$order=$this->hasMany(Order::className(),['customer_id'=>'id'])->asArray()->all();
return $order;
}
}
Order.php 内容
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Order extends ActiveRecord{
public function getCustomer(){
//$this 相当于 实例化的order对象
$customer=$this->hasOne(Customer::className(),['id'=>'customer_id'])->asArray()->all();
return $customer;
}
}
控制器HelloController.php内容
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
use yii\web\Cookie;
use app\models\Test;//导入text的命名空间
use app\models\Customer;//导入Customer的命名空间
use app\models\Order;//导入Order的命名空间
class HelloController extends Controller{
public function actionIndex(){
//多表关联查询 新建 customer order 的 模型类 并且在index 方法上面 use 命名空间
//根据客户查 订单 一对多 hasMany
// 首先查出客户的信息 操作 customer 模型类
// $customer=Customer::find()->where(['name'=>'李四'])->one(); //
one() 就没有外面的的数组 直接是对象 得到用户的所有信息
// // $order=$customer->getOrder();//
第一种方法 getOrder方法在customer模型类中
// $order=$customer->order; //第二种方法 调用不存在属性时 会在找 一个方法 get拼接这个属性名 即还是 getOrder()这个方法 所有在模型类中方法格式 是get某某
//根据订单查 客户 一对一 hasOne
// $order=Order::find()->where(['id'=>'1'])->one();
// $customer=$order->getCustomer();
}
}
11 关联查询性能问题
控制器HelloController.php内容
<?php
namespace app\controllers;//命名空间要写 use 前面 不然会报错
use yii\web\Controller;
use yii\web\Cookie;
use app\models\Test;//导入text的命名空间
use app\models\Customer;//导入Customer的命名空间
use app\models\Order;//导入Order的命名空间
class HelloController extends Controller{
public function actionIndex(){
//关联查询性能问题
//1
关联查询结果缓存
// $customer=Customer::find()->where(['name'=>'李四'])->one();
// $order=$customer->order; //执行的sql语句 是 select *from order where customer_id=''';
// //此时 查询结果在缓存中 要是数据库 数据发生改变 $order2=$customer->order; 再次查询是 数据还是上次的数据 所以要清空上次的数据
// unset($customer->order;);
// $order2=$customer->order;
//2
关联查询的多次查询
// 不加where 条件 就是把所有的客户都查询出来 假设有100个客户 sql 语句时 select *from customer;
// $customer=Customer::find()->all();
// foreach($customer as $aaa){
// $order=$aaa->order;
// } //sql 语句是 select *from order where customer_id =$aaa; 执行了 100次
// 这样就执行了 101次查询
//解决方案 测试的没有用啊 会报错
// $customer=Customer::find()->with('order')->all(); //产生的sql 语句是 select *from customer; select *from order where customer_id in(.......一个客户表id的集合 );
// foreach($customer as $aaa){
// $order=$aaa->order;
// }
}
}