第一个helloword请求
下面我们来写第一个yii的helloworld,首先进入controllers/创建HelloController.php,写入如下代码
<?php
namespace app\controllers;
use yii\Web\Controller;
class HelloController extends Controller{
public function actionIndex(){
echo 'Hello World';
}
}
复制代码
namespace 使用app\controllers,类HelloController 继承yii\web\Controller,在类中定义请求actionIndex,实现一个输出hell world的请求,访问地址为http://localhost:8888/index/r=hello/index
,yii规定请求使用r=(controller类名)/(action方法后缀)的形式访问。 效果如下
返回一个php页面
接下来,我们来做一个新的php页面,在/views下创建hello文件夹,并创建say.php文件,写入简单的一段html代码即可
<p>say hi</p>
复制代码
然后在HelloController.php下添加actionSay
public function actionSay(){
return $this->render('say');
}
复制代码
此时通过$this-render('say');方法,yii框架就会自动指向刚刚在views下创建的hell/say.php页面了。
细心的人会看到返回页面会自带导航及底部工具栏,这是yii框架自带的一些页面,对应代码可以在views/sitex/下查看具体代码。