一、控制器文件:TestController.php
1 <?php
2 //1、命名空间使用和TP一致的
3 //定义命名空间
4 namespace app\controllers;
5 //引入基础控制器
6 use yii\web\Controller;
7 class TestController extends Controller{
8 // 2、action的定义位置相反
9 public function actionIndex(){
10 echo ‘hello yii2lover‘;
11 //重定向
12 $this->redirect([‘site/index‘]);
13 //去首页
14 $this->goHome();
15 //返回上一级
16 $this->goBack();
17 //刷新当前页面---会出问题--网页包含重定向循环
18 $this->refresh();
19 //5、使用视图的规则不一样
20 //使用布局模板render(给予)
21 return $this->render(‘index‘,[‘data‘ => [1,2,3] ] );
22 //不使用布局模板partial(局部的)
23 return $this->renderpartial(‘index‘,[‘data‘ => [1,2,3] ] );
24 }
25 // 3、当名字为驼峰法俩单词组成时,则请求是r=test/show-user
26 public function actionShowUser(){
27 echo ‘您是坤哥吗‘;
28
29 }
30 // 4、请求URL本身的写法的区别index.php?r=控制器名/方法名
31 }
二、视图文件:views/test/index.php
1 test-index
2 <br>
3 <!-- 6、PHP在模板中没有使用模板引擎的语法 -->
4 <?php
5 foreach ($data as $val){
6 echo $val ,‘<br>‘;
7 }
8 ?>
三、要点:
原文:https://www.cnblogs.com/baobaoa/p/9360051.html