牛骨文教育服务平台(让学习变的简单)

向View传值

Models, Views, Controllers

http://www.yiichina.com/doc/guide/2.0/structure-overview

向View传递Model

项目代码下载 https://git.oschina.net/radarxu/yii-demo

1、简单模型

frontendcontrollersSiteController.php

public function actionAbout()
{
    $location = "中国,上海";
    return $this->render("about", ["location" => $location]);
}

frontendviewssiteabout.php

/* @var $location string */

<p>地址: <?= $location ?></p>

2、数组模型

frontendcontrollersSiteController.php

public function actionAbout()
{
    $location = ["city" => "上海", "country" => "中国"];
    return $this->render("about", ["location" => $location]);
}

frontendviewssiteabout.php

/* @var $location array */

<p>城市: <?= $location["city"] ?></p>
<p>国家: <?= $location["country"] ?></p>

3、复杂模型

frontendcontrollersSiteController.php

public function actionAbout()
{
    $model = new AboutModel();
    $model->name = "徐飞";
    $model->city = "上海";
    $model->country = "中国";
    return $this->render("about", ["model" => $model]);
}

frontendviewssiteabout.php

/* @var $model frontendmodelsAboutModel */

<p>姓名: <?= $model->name ?></p>
<p>城市: <?= $model->city ?></p>
<p>国家: <?= $model->country ?></p>

4、多个模型

frontendcontrollersSiteController.php

public function actionAbout()
{
    $name = "徐飞";
    $city = "上海";
    $country = "中国";
    return $this->render("about", ["name" => $name, "city" => $city, "country" => $country]);
}

frontendviewssiteabout.php

/* @var $name string */
/* @var $city string */
/* @var $country string */

<p>姓名: <?= $name ?></p>
<p>城市: <?= $city ?></p>
<p>国家: <?= $country ?></p>