jw-de
jw-de

Reputation: 39

How do I fix the Yii2: view file does not exist error?

I am trying to render a view via an Action but yii2 throws an error at me. I have tried debugging it but I have not found anything. here is my code: The controller:

<?php

namespace app\controllers;

use yii\web\Controller;

class CalculatorController extends Controller
{
    public function actions()
    {
        return [
            'show' => 'app\controllers\frontend\calculator\ShowAction',
        ];
    }
}

The action:

<?php

namespace app\controllers\frontend\calculator;

use yii\base\Action;

class ShowAction extends Action
{
    public function run()
    {
        return $this->controller->render('frontend/calculator/show');
    }
}

the view:

<?php

namespace app\controllers\frontend\calculator;

use yii\base\Action;

class ShowAction extends Action
{
    public function run()
    {
        return $this->controller->render('frontend/calculator/show');
    }
}

The error:

View not Found – yii\base\ViewNotFoundException The view file does not exist: C:\xampp\htdocs\basic\views\calculator\frontend/calculator/show.php

But the correct path to the view would be:

C:\xampp\htdocs\basic\views\frontend/calculator/show.php

I have no idea where the extra "calculator" came from. thank you in advance!

EDIT: so if I type ../ before the view path it is working. but I stil want to know why this weird stuff is happening...

Upvotes: 1

Views: 1158

Answers (1)

Mike
Mike

Reputation: 9

'frontend/calculator/show' is the relative path so it will be searched under current controller.
Your additional 'calculator' part is the controller name.

Upvotes: 1

Related Questions