视图安全
与mysql注入安全问题类似,YII也可以对用户输入内容进行处理以防止注入攻击。
$data['name'] = 'lee<script>alert(4);</script>',
这里是一个js的弹窗,如果不加处理,代码就会被当成js处理而出现一个弹窗。 1.使用命名空间为yii\helpers\Html的Html下的encode方法,处理后js代码就会被转义成普通字符串,而不是js脚本。
<?php
use yii\helpers\Html;
?>
<h1><?=Html::encode($name);?></h1>
2.使用命名空间为yii\helpers\HtmlPurifier的HtmlPurifier下的process方法,处理后js代码就会过滤掉,只剩下lee这个普通串。
<?php
use yii\helpers\HtmlPurifier;
?>
<h1><?=HtmlPurifier::process($name);?></h1>
布局文件
碰到需要重复使用的代码时,可以存为布局文件,多次调用。 1.在/view/layout下放置布局文件common.php,文件内容如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<h1><?=$content;?></h1>
</body>
</html>
2.在控制器中引用布局文件
public $layout = 'common';
3.通过render方法(render方法有第二个参数可以用来传值给被调用的文件)将视图文件index.php的内容存入$content(默认),且把$content传到布局文件中,输出布局文件。
$this->render('index');
数据库配置
类似于微型框架,YII的数据库账户、密码等信息也是在config文件中配置的,具体在config文件夹下的db.php文件。
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2',
'username' => 'root',
'password' => 'root',
'charset' => 'utf8',
];
之后在models文件夹新建数据库模块Heber.php继承自命名空间为yii\db\ActiveRecord的ActiveRecord类,注意新建模块也要为它设置命名空间
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Heber extends ActiveRecord{
}
然后在控制器中就可以使用各种封装好的数据库方法了,这里选几种常用的,更多可参考yii手册。
数据库查询
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Test;
class HelloController extends Controller{
public function actionIndex(){
//最基础、最通用的直接执行完整sql语句
$sql = 'select * from test where id = 1';
$result = Test::findBySql($sql)->all();
//条件查询,id=1
$result_1 = Test::find()->where(['id'=>1])->all();
//条件查询,id=1 且结果存为数组
$result_2 = Test::find()->where(['id'=>1])->asArray->all();
//条件查询,id>1
$result_3 = Test::find()->where(['>','id',1])->all();
//条件查询,3>=id>=1
$result_4 = Test::find()->where(['between','id',1,3])->all();
//条件查询,id中有1的
$result_5 = Test::find()->where(['like','id',1])->all();
}
}
?>
数据库删除
还有删除操作
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Test;
class HelloController extends Controller{
public function actionIndex(){
$sql = 'select * from test where id = 1';
$result = Test::findBySql($sql)->all();
//删除
$result[0]->delete();
//另一种删除
Test::deleteAll('id>0');
}
}
?>
数据库插入
往表名为heber的数据表的title字段插入值(hello)
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Heber;
class HelloController extends Controller{
public function actionIndex(){
$test = new Heber;
$test->title = 'hello';
$test->save();
}
}
?>
在插入前,还可以使用验证器来检验输入是否规范,在heber.php模型文件中使用下列代码
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Heber extends ActiveRecord{
public function rules(){
return [
//id是否为int型变量
['id','integer'],
//title是否为string变量且长度在0——5个字节间
['title','string','length'=>[0,5]]
];
}
}
然后在控制器中使用代码调用及判断是否出错。注意:调用验证器要在插入之前
$test->validate();
if($test->hasErrors()){
echo 'data is error;'
}
else{
$test->save();
}
##数据库修改 先取出数据库中的数据存入一个变量中,再在变量中修改,然后保存
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Heber;
class HelloController extends Controller{
public function actionIndex(){
$test = new Heber;
//取出id为5的数据,存入$data中
$data = $test->find()->where(['id'=> '5'])->one();
$data->title = 'cha';
$data->save();
}
}
?>
##数据库关联查询 在models文件夹里新建两个文件customer.php和order.php,在数据库中建两张名为cus和order的表。 1.customer.php
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Customer extends ActiveRecord{
public function getOrders(){
//使用hasMany方法关联查询多条结果,第一个参数是另一张表2的表名,第二个参数是关联的字段,注意,表2的字段在前。
return $this->hasMany(Order::classname(),['customer_id'=>'id'])->asArray();
}
}
2.order.php
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Order extends ActiveRecord{
public function getCustomer(){
//使用hasOne方法来关联查询单条结果,与上类似
return $this->hasOne(Customer::classname(),['id'=>'customer_id'])->asArray();
}
}
3.HelloController.php
<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\Heber;
use app\models\Customer;
use app\models\Order;
class HelloController extends Controller{
public function actionIndex(){
// $customer = Customer::find()->where(['id'=>1])->one();
// $orders = $customer->orders;
// print_r($orders);
//先查询要关联查询的订单,再获取对应的customer,YII会自动将customer加上get拼接成getCustomer来使用order.php文件中的getCustomer方法。
$orders = Order::find()->where(['id'=>2])->one();
$customer = $orders->customer;
print_r($customer);
}
}
?>