Search code examples
yiiyii2yii2-basic-app

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


I use basic template of yii, i create a new controller manually in Controllers Folder and name it CountryController.php and put bellow codes in it.

<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;

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

But when i type http://localhost/sites/basic/web/index.php?r=country%2Findex in browser i get following Exception:

View not Found yii\base\ViewNotFoundException

This is image of the error

On the other side when i type http://localhost/sites/basic/web/index.php?r=site%2Findexin the browser it renders index view correctly.

Why this happen? can i create a Controller manually? How?


Solution

  • The error message is not saying it can't find the page, just that it can't find the view file. Along with your controller, you should also have created a view file app\views\country\index.php This is the file you are referring to when you call `$this->render('index').