一盯腌、Controller:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\web\Cookie;
/**
* chen.jian
*/
class HelloController extends Controller{
public $layout = 'common';
public function actionIndex(){
// 一弓摘、請(qǐng)求組件
$request = Yii::$app->request; //Yii全局類,$app靜態(tài)變量刃宵,用來(lái)加載應(yīng)用主體衡瓶,例如request組件;
echo $request->get("name"); //獲取get方式的參數(shù)為name的值牲证,同理post;
echo $request->get("name", "majiali"); //獲取get方式的參數(shù)為name的值哮针,如果沒(méi)有name的值,默認(rèn)就是“majiali”坦袍,同理post;
echo $request->isGet; //是否進(jìn)行了get方式的提交true Or false十厢,同理post;
echo $request->userIp; //獲取用戶的IP地址;
// 二、響應(yīng)組件
$response = Yii::$app->response;
//自定義返回的狀態(tài)碼捂齐。但是好像不常用
$response->statusCode = '404';
//常用蛮放, 給responce相應(yīng)的header頭里面添加、修改奠宜、刪除消息包颁;
$response->headers->add("pragma", "no-cache");
$response->headers->set("pragma", "max-age=5");
$response->headers->remove("pragma");
//響應(yīng)后跳轉(zhuǎn)
$response->headers->add("location", "http://baidu.com");
//或者使用內(nèi)置的:
$this->redirect("http://baidu.com", 302);
//響應(yīng)后文件下載
$response->headers->add("content-dispostion", "attachment; filename='a.jpg'");
//或者使用內(nèi)置的:
$this->sendFile('./robots.txt');
// 三缝其、session組件
$session = Yii::$app->session(); //此時(shí)$session是對(duì)象
$session->isActive; //session是否開(kāi)啟;
$session->open(); //開(kāi)啟session徘六;
$session->set("user", "chenjian"); //存session, 去php.ini中查找session.save_path查看session存在哪里内边。
$session->get("user"); //獲取session;
$session->remove("user"); //刪除session待锈;
//或者以數(shù)組的形式來(lái)操作session
$session["user"] = "chenjian"; //設(shè)置session
echo $session["user"]; //獲取session
unset($session["user"]); //刪除session
// 四漠其、cookie集合,其實(shí)屬于response響應(yīng)組件
$cookie = Yii::$app->response->cookies;
$data = array('name' => "user", 'value'=>"chenjian");
$cookie->add(new Cookie($data));
$cookie->remove('name');
echo Yii::$app->request->cookies->getValue('user', "majiali"); //獲取cookie的值.如果沒(méi)有值那么返回的默認(rèn)值就是“majiali”
// 五竿音、加載視圖之傳遞參數(shù)
$info = array(
'name' => "chenjian",
'jober' => "PHPer",
'age' => 28,
'majiali'=>['A', 'B']
);
return $this->renderPartial('index', $info); //視圖里面接受數(shù)組的一個(gè)key和屎,例如$name;
// 六、加載視圖之布局文件春瞬, 對(duì)于有公用的代碼柴信,可以使用布局文件
// 1. actionIndex方法外定義一個(gè)變量public $layout = 'common';, 表示加載布局文件
// 2. 創(chuàng)建views/layouts/common.php的布局文件宽气,里面使用 $content變量來(lái)表示render方法傳遞的視圖;
return $this->render('index'); //其實(shí)render()方法把視圖放在$content變量里面随常,這個(gè)變量可以傳遞給布局文件。
// 七萄涯、加載視圖之布局文件之?dāng)?shù)據(jù)塊 ------ 類似于覆
}
}
二绪氛、View文件下的視圖文件:
2.1 普通視圖文件: views/hello/index.php
<?php
use yii\helpers\Html;
use yii\helpers\HtmlPurifier;
// 1. 一般顯示
echo "sadasd";
// 2. 加載控制器傳來(lái)的數(shù)據(jù),并且對(duì)傳遞過(guò)來(lái)的代碼進(jìn)行了處理防止代碼攻擊涝影。
// 控制器中`$data=array('name'=>'ChenJian'); return $this->render('index', $data); `
echo Html::encode($name); //注意枣察,使用的是數(shù)組里面的一個(gè)key
echo HtmlPurifier::process($name);
// 3. 加載另外一個(gè)視圖
echo $this->render('about');
?>
2.2 布局文件:views/layouts/common.php
<?php
<!DOCTYPE html>
<html>
<head>
<title>Common</title>
</head>
<body>
<h1>Hello, i am Common</h1>
<?php echo $content; //$content來(lái)自控制器 ?>
</body>
</html>
?>
2.3 數(shù)據(jù)塊:views/hello/index.php
<!-- index.php 中定義數(shù)據(jù)塊 -->
<?php $this->beginBlock("block1"); ?>
<h1>新的數(shù)據(jù)來(lái)代替布局文件中的數(shù)據(jù)</h1>
<?php $this->endBlock(); ?>
<!-- common.php(布局文件)中使用數(shù)據(jù)塊 -->
<?php $this->blocks['blocks1']; ?>
三、數(shù)據(jù)庫(kù)的使用:
控制器中:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use yii\web\Cookie;
use app\models\Hello;
use app\models\Country;
/**
* chen.jian
*/
class HelloController extends Controller{
public function actionIndex1(){
$sql = 'select * from country';
$result = Hello::findBySql($sql);
echo "<pre>";
var_dump($result->all());
}
public function actionIndex2(){
//防止sql注入燃逻,使用了占位符的概念序目,條件中name是$name的變量
$name = "Australia";
$sql = "select * from country where name=:name";
$result = Hello::findBySql($sql, array(':name'=>$name));
var_dump($result->all());
}
public function actionIndex3 (){
// 1. name等于Australia
$result1 = Hello::find()->where(['name' => "Australia"]);
// 2. id大于6
$result2 = Hello::find()->where(['>', 'id', 6]);
// 3. id介于6和10
$result3 = Hello::find()->where(['between', 'id', 6, 10]);
$condition = array('name' => "Australia" );
$result4 = Hello::find()->where($condition); //Object
$result4 = Hello::find()->where($condition)->all(); //Object
echo("<pre>");
var_dump($result4);
}
public function actionIndex4(){
//1. 查詢結(jié)果轉(zhuǎn)為數(shù)組,去除不必要的數(shù)據(jù)伯襟。
$result = Hello::find()->where(['name' => "Australia"])->asArray(); //修正后的Array
var_dump($result->all());
//2. 批量查詢猿涨。為了保護(hù)內(nèi)存,每次查2條逗旁。
$result = Hello::find()->asArray();
foreach ($result->batch(2) as $t) {
var_dump($t); //
echo count($t); // 依次輸出22222嘿辟,因?yàn)橐还?0條數(shù)據(jù)舆瘪。
}
}
public function actionIndex5(){
// 1. 刪除數(shù)據(jù)片效, 調(diào)用對(duì)象的delete方法
$result = Hello::find()->where(['code' => 'te'])->all(); //Array
var_dump($result[0]->delete());
// 2. 刪除全部,或者根據(jù)條件
Hello::deleteAll(); //刪除全部
Hello::deleteAll('code = "te"'); //根據(jù)條件
}
public function actionIndex6(){
//1. 增加數(shù)據(jù)并且驗(yàn)證
$hello = new Hello();
$hello->code = "CJ22";
$hello->name = "chenjian";
$hello->validate(); //驗(yàn)證英古,返回boolean
$hello->hasErrors(); //是否有錯(cuò)誤淀衣,返回boolean
var_dump($hello->save());
}
public function actionIndex7(){
//修改數(shù)據(jù)
$result = Hello::find()->where(['code' => "CJ"])->one();
$result->population = 888888;
var_dump($result->save());
}
public function actionIndex8(){
//聯(lián)合查詢
$result = Hello::find()->where(['code' => "CJ"])->one();
$data = $result->hasMany('app\models\Country', ['c_code' => 'code'])->all();
echo "<pre>";
var_dump($data);
}
}
模型中;
<?php
namespace app\models;
use yii\db\ActiveRecord;
/**
* chen.jian
*/
class Hello extends ActiveRecord{
public static function tableName(){
return 'country';
}
public function rules(){
return [
['code', 'string', 'length'=>[0, 2]]
];
}
}