手动创建一个新的 Controller 并在 Yii 中遇到 "View not Found" 错误

Create a new Controller manually and face with "View not Found" error in Yii

我使用 yii 的基本模板,我在 Controllers 文件夹中手动创建一个新控制器并将其命名为 CountryController.php 并在其中放入以下代码。

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;

class CountryController extends Controller
{
    public function actionIndex()
    {
        return $this->render('index');
    }
}

但是当我在浏览器中键入 http://localhost/sites/basic/web/index.php?r=country%2Findex 时,出现以下异常:

View not Found yii\base\ViewNotFoundException

This is image of the error

另一方面,当我在浏览器中输入 http://localhost/sites/basic/web/index.php?r=site%2Findex 时,它会正确呈现索引视图。

为什么会这样?我可以手动创建一个控制器吗?怎么样?

错误信息并不是说找不到页面,只是找不到视图文件。除了你的控制器,你还应该创建一个视图文件 app\views\country\index.php 这是你在调用 `$this->render('index').

时所指的文件