<?php
namespace app\controllers;
use yii\web\Controller;
/**
-
控制器之请求处理
*/
class HelloController extends Controller
{
$request = \YII::$app->request;echo $request->get('id');
echo $request->post('name',3333);
if($request->isget){
echo 'is get method!';
}echo $request->userIp;
}
/**
-
控制器之响应处理
*/
class HelloController extends Controller
{
$res = \YII::$app->response;$res->statusCode = '404'; //设置状态码
$res->headers->add('pragma','no-cache'); //设置缓存
$res->headers->set('pragma','max-agge=5'); //设置缓存时间
$res->headers->remove("pragma");$res->headers->add('location','http://www.baidu.com'); //设置跳转
$this->redirect('http://www.baidu.com',302); //控制器中直接设置跳转
$res->headers->add('content-disposition','attachment; filename="a.jpg"'); //下载文件
$res->sendFile('./b.jpg'); //包装的方法来下载文件
}
/**
-
控制器之session处理
*/
class HelloController extends Controller
{
$session = \YII::$app->session;//对象形式
$session->open(); //打开session
$session->set('user','张三'); //设置
$session->get('user'); //获取
$session->remove('user'); //删除//数组形式
$session['user'] = '张三'; //设置
echo $session['user']; //获取
unset($session['user']); //删除
}
/**
-
控制器之cookie处理
*/
class HelloController extends Controller
{
public function actionIndex{
$cookies = \YII::$app->response->cookies;$cookie_data = array('name'='user','value'=>'张三'); $cookies->add(new Cookie($cookie_data)); //设置cookie $cookies->remove('id'); //删除cookie $cookies = \YII::$app->request->cookies; //获取cookies值 $cookies->getValue('user',20); //获取cookie值
}
}