yii2基础知识点
一、命名空间
<?php
//app指的是网站根目录
namespace app\controllers;
//yii指的是 vendor\yiisoft\yii2\web
use yii\web\Controller;
二、路由与控制器和方法的关系
(一)http://xxx.com/index.php?r=home/user-info
1、home是指控制器
2、user-info是指方法(驼峰名称的方法,在路由上全部小写,并且使用【-】拼接)
<?php
namespace app\controllers;
use yii\web\Controller;
class HomeController extends Controller
{
//名称规则
//路由:http://johnliyii.com/index.php?r=home/user-info
//方法名:actionUserInfo
//1、必须加入前缀action
//2、再拼接方法
//3、驼峰名称方法,路由需要使用【-】来凭借
public function actionUserInfo()
{
return $this->render('userinfo');
}
}
三、控制内置函数
(一)重定向
$this->redirect('site/index');
(二)回到首页
$this->gohome();
(三)返回上一页
$this->goback();
四、视图渲染
(一)单纯渲染
return $this->renderPartial('index');
(二)嵌套渲染
return $this->render('index');