YII基础学习笔记

Controller

实现在HelloController.php中编写

1
2
3
4
5
6
7
8
9
10
11
<?php

namespace app\controllers;
use yii\web\Controller;

class HelloController extends Controller{
public function actionIndex(){
echo 'hello word';
}

}

http://127.0.0.1/basic/web/index.php?r=hello/index 访问成功

获取get和post 请求内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

namespace app\controllers;
use yii\web\Controller;

class HelloController extends Controller{
public function actionIndex(){
$request = \YII::$app->request;//全局类
echo $request->get('id',20); //当没有进行get时 默认为20


}

}

同样也可以 $request->post(‘id’,20);进行post

YII操作HTTP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php

namespace app\controllers;
use yii\web\Controller;

class HelloController extends Controller{
public function actionIndex(){
// $request = \YII::$app->request;//全局类
// echo $request->get('id',20); //当没有进行get时 默认为20
$res = \YII::$app->response;
// 添加头部信息
$res->headers->add('pragma','no-cache');
$res->headers->set('pragma','no-cache');
$res->headers->remove('pragma');

// 实现跳转
$res->headers->add('location','http://www.baidu.com');
// 有专门跳转方法
$this->redirect('htttp://wwww.baidu.com',302);

//进行文件下载
// 访问文件默认在 WEB目录下

$res->headers->add('content-dispostion','attachment; filename="a.jpg"')
$res->sendFile('./robots.txt');

}

}

YII 操作Session

session 的存储路径可在 php.ini 中找到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

namespace app\controllers;
use yii\web\Controller;

class HelloController extends Controller{
public function actionIndex(){
$session = \YII::$app->session;
$session->open(); //用于开启session
// if($session->isActive){ //用于检测 session 是否开启
// echo "session is active";
// }

$session->set('user','夏利'); //用于设置 session

}

}

1
2
3
4
5
6
7
echo $session->get('user');  // 获得session值

$session->remove('user');

$session['user'] = '张三';

unset($session['user']);

YII 操作Cookie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php

namespace app\controllers;
use yii\web\Controller;
use yii\web\cookie;

class HelloController extends Controller{
public function actionIndex(){
$cookies = \YII::$app->response->cookies;

$cookie_data = array('name'=>'user','value'=>'zhangsi');

$cookies->add(new cookie($cookie_data));





}

}

View 层

要访问视图文件 需要把视图文件放到与控制器名相同的文件夹下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

namespace app\controllers;
use yii\web\Controller;
use yii\web\cookie;

class HelloController extends Controller{
public function actionIndex(){
return $this->renderPartial('index');






}

}

.\basic\views\Hello.php

1
2
3
<?php

echo "<h1>小姐姐</h1>";

访问http://127.0.0.1/basic/web/index.php?r=hello/index
成功得到视图文件

当要在view文件中使用控制器中的的变量时 需要把数据放入数组当中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php

namespace app\controllers;
use yii\web\Controller;
use yii\web\cookie;

class HelloController extends Controller{
public function actionIndex(){

$hello_str = '小哥哥';
$test_array = array(1,2);

//创建一个数组 存放数据
$array = array();

// 将数据放入数组
$data['view_str'] = $hello_str;

$data['view_array'] = $tset_array;

return $this->renderPartial('index',$data);






}

}
1
2
<h1><?=$view_str;?></h1>
<h1><?=$view_array[0];?></h1>

当$hello_str 可控 会造成恶意代码攻击 可以使用两个方法进行过滤

1
$hello_str = '小姐姐<script>alert(123)</script>';
1
2
3
4
5
6
7
8
9
10
<?php
use yii\helpers\Html; // 这个类会将script 转移

use yii\helpers\HtmlPurifier; //这个类会将script 直接过滤掉

?>

<h1><?=Html::encode($view_str);?></h1>

<h1><?=HtmlPurifier::process($view_str);?></h1>

优化布局
当在hello文件夹中有about.php index.php

1
2
3
4
5
6
7
8
9
10
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
hello about
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
hello index

</body>
</html>

可见 除了 hello about hello index 其他的都显得 多余
接下来把 多余的代码 放在basic\views\layouts 文件夹下 并创建一个common.php
内容为

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1>hello common</h1>
<?=$content;?>
</body>
</html>

index.php和about.php直接写hello index hello about

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php

namespace app\controllers;
use yii\web\Controller;
use yii\web\cookie;

class HelloController extends Controller{
public $layout = 'common'; //指定模板
public function actionIndex(){
// 调用render方法
return $this->render('index');
}
}

访问http://127.0.0.1/basic/web/index.php?r=hello/index

如果想在index.php视图访问about.php视图文件

如果访问另一个视图文件的变量

YI操作数据块
当不想显示模板中的一些内容时 用
$this->endBlock();
来定义一个html代码块,该代码块可在layout文件中引用,
$this->blocks[‘block_name’]

Model

YII 数据模型

对数据库进行增删改查操作数据库
实现创建数据库yii 表test tittle id 字段

在config文件夹中的db.php配置好数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
namespace app\controllers;
use yii\web\Controller;
usw app\models\Test;

class HelloControllers extends Controller{
public function actionIndex(){
$sql = 'select * from test where id=:id';

// 首先产生的每一条记录被放在一个对象里面 每一个对象被放在数组里面
$results = Test::findBySql($sql,array(':id'=>1))->all(); // 利用占位符 可以防止sql注入 无论拼接的是什么 都会当成一个字符

}
}


?>

但是这样看起来代码有点多 可以用find()方法 将查询条件放到数据中去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
namespace app\controllers;
use yii\web\Controller;
usw app\models\Test;

class HelloControllers extends Controller{
public function actionIndex(){
// $sql = 'select * from test where id=:id';
//
// // 首先产生的每一条记录被放在一个对象里面 每一个对象被放在数组里面
// $results = Test::findBySql($sql,array(':id'=>1))->all(); // 利用占位符 可以防止sql注入 无论拼接的是什么 都会当成一个字符

$results = Test::find()->where(['id']=>1)->all();
}
}


?>

这样就显得简洁多了
如果要查询id>0 的话就要用第三个参数

1
$results = Test::find()->where(['>','id','0'])->all();

查询id>=1 id<=2

1
$results = Test::find()->where(['between','id',1,2])->all();

like 操作

1
$results = Test::find()->where(['linke','title','title'])->all();

用asArray 可以降低农村使用量和使代码,数据清晰

1
$results = Test::find()->where(['linke','title','title'])->asArray()->all();

批量查询

1
2
3
foreach(Test::find()->batch(1) as $tests){
printf(count($tests)) // 每次操作一条数据 将结果放在$tests
}

进行删除操作

1
2
3
4
5
$results = Test::find()->where(['id']=>1)->all();
$results[0]->delete();

//第二中方法,及利用占位符
Test::deleteAll('id>:id',array(':id'=>0)); //

进行增加数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
namespace app\controllers;
use yii\web\Controller;
usw app\models\Test;

class HelloControllers extends Controller{
public function actionIndex(){
$test = new Test;
$test->id = 3;
$test->title = 'title3';
$test->save();

}
}


?>

接下来使用rules 进行验证
model文件夹下的Test.php内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Test extends ActiveRecord{
public function rules(){
return [
['id','integer'],
['tittle','string','length'=>[0,5]]
];
}
}


?>

控制器HelloController.php代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Test;

class HelloController extends Controller{
public function actionIndex(){
$test = new Test;
$test->id = 'zxc';
$test->tittle = 'title3';
$test->validate();
if($test->hasErrors()){
echo "data is error";
die;
}

$test->save();

}
}


?>

当id 为字符时 不满足验证器0-5 的访问 返回data error

接下来进行修改操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Test;

class HelloController extends Controller{
public function actionIndex(){
$test = Test::find()->where(['id'=>1])->one(); //取出一角数据放在noe 而不是all()
$test->tittle = 'title4'; // 进行修改
$test->save(); // 然后进行保存



}
}


?>

可以加个判断

接下来进行 关联查询
所谓关联查询 就是 比如有两张表 1顾客表 2订单表 多个订单对应一个顾客 形成一对多的关系 一个订单对应 一个顾客 形成一对一的关系



接下来编写 order.php customer.php

成功得到数据

但是在控制器进行数据库的操作不满足mvc 在model类 进行封装一个 操作数据库的类

在进行查询的时候 会把查询内容放到select 语句中,当这个数据更新时不会再进行select 而是拿上次slect 的结果 所以如果要再进行select 时 需要用unset()释放那个结果