入门

PPG007 ... 2023-8-29 About 1 min

# 入门

# 安装

首先需要安装包管理器 Composer (opens new window),Linux 中可以执行以下命令:

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
1
2

然后使用下面的命令创建 Yii 应用程序模板:

composer create-project --prefer-dist yiisoft/yii2-app-basic backend
1

这个命令将会在当前路径的 backend 目录中安装 Yii 应用程序模板的最新稳定版本,如果超时报错可以参考这里 (opens new window)换源或者使用代理:

HTTP_PROXY=http://127.0.0.1:8889 composer create-project --prefer-dist yiisoft/yii2-app-basic backend
1

执行下面的命令开启 server:

php yii serve --port=8888
1

# Hello World

首先在一个 controller 里编写一个接口:

public function actionHello($message = 'Hello')
{
    return $message;
}
1
2
3
4

然后访问:https://hostname/index.php?r=site/say&message=Hello+World 即可获取返回值。

也可以直接返回视图:

// 在 views/site 目录下创建一个 hello.php
<?php
use yii\helpers\Html;
?>
<?=Html::encode($message) ?>
// controller
public function actionHello($message = 'Hello')
{
    return $this->render('hello', ['message' => $message]);
}
1
2
3
4
5
6
7
8
9
10

# 表单

首先创建一个模型用于接收和校验数据:

<?php
namespace app\models;

use yii\base\Model;

class EntryForm extends Model
{
    public $name;
    public $email;
    public function rules()
    {
        return [
            [['name', 'email'], 'required'],
            ['email', 'email'],
        ];
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

上面 rules 限制了这个类的对象必须要有 name 和 email 属性,并且 email 需要是邮箱格式。

然后编写 controller:

public function actionEntry()
{
    $model = new EntryForm();
    // 要么指定 form-data 的 EntryForm 字段,要么第二个参数传空,原因见 load 方法
    if ($model->load(Yii::$app->request->post(), '') && $model->validate()) {
        return 'ok';
    }
    throw new BadRequestHttpException('invalid params');
}
1
2
3
4
5
6
7
8
9

post 请求需要在 header 里加 X-CSRF-Token 字段,这个值可以通过 get 方法调用下面的 action 获取:

public function actionGetToken()
{
    return Yii::$app->request->getCsrfToken();
}
1
2
3
4

也可以配置 request 组件关闭 csrf 校验:

'request' => [
    'enableCsrfValidation' => false,
],
1
2
3
Last update: September 4, 2023 10:00
Contributors: Koston Zhuang