- yii2通过require(DIR…/models/xx.php)方式引入类的方式
require_once('../models/phpqrcode.php');
$qrcode=new \QRcode();
$qrcode::png('http://xxxx/');
其中QRcode为我引入的php文件中定义的类,实例化使用时需要在类名前添加反斜杠,否则会报app/models/QRcode not found
此外查询资料发现一些比较好的引入文件的方法,但并未尝试
用namespace+autoload,看文档自动加载章节
或者
$this->render,因为不会有变量名冲突,不建议直接include或require
2.yii2继承ActiveRecord的类,在处理数据库操作时,自带防SQL语句注入功能,因此不需要自增安全措施
如下是防JS语句注入功能
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
echo Html::encode($view_hello_str) //可以原样显示<script></script>代码
echo HtmlPurifier::process($view_hello_str) //可以过滤掉<script></script>代码
3.yii2中一些比较常用的数据库操作
use app\models\user
User::find()->where(['property'=>$username])->one();取出一条
User::find()->where(['property'=>$username])->all();取出所有
User::find()->where(['property'=>$username])->asArray()->one();yii2取出数组格式的数据
User::find()->where(['property'=>$username])->orderBy('property asc')->one();按升序取数据,默认降序
User::find()->where(['property1'=>$username,'property2'=>$userid])->one();yii2联合查询
User::find()->where(['property'=>$username])->andwhere('<>','property','$username')->one();不方便使用上一种方法时可以考虑的,此处<>意为不等于