yii框架控制器的请求参数处理

//命名空间
namespace app\controllers;
//需要继承框架的控制器
use yii\web\Controller;
//定义类
class HelloController extends Controller{
	//操作,以action开头,加上操作名称
	public function actionIndex(){
		//YII是全局类,YII全局类中有个静态变量$app,这个$app是应用主体,开始时$app会加载各种应用组件
		//获取全局类中的请求组件
		$request = \YII::$app->request;

		//请求参数通过请求组件request获取,get方式传递的参数通过get方法获取,post方式传递的参数通过post方法获取
		//get方法获取参数id,id的默认值是10
		echo $request->get('id', 10) . '
'; //post方法获取参数name,name的默认值hello echo $request->post('name', 'hello') . '
'; //判断请求方式 if($request->isGet){ echo 'this is get method' . '
'; } if($request->isPost){ echo 'this is post method' . '
'; } //获取请求ip地址 echo $request->userIp; } }

 

你可能感兴趣的:(yii框架)