Yii2 错误:视图文件不存在 [英] Yii2 error: The view file does not exist
问题描述
我已经使用 Yii2 几个星期了,并且已经掌握了它的窍门.然而,今天,不知为何,Yii 一直把我路由到错误的页面,导致错误,因为没有找到网页:
I've been using Yii2 for a few weeks now, and getting the hang of it. However, today, for a reason I do not know, Yii's been routing me to the wrong page, causing errors because web pages have not been found:
URL:
http://localhost/web/index.php?r=site/index
Error:
Invalid Parameter – yii\base\InvalidParamException
The view file does not exist: C:\xampp\htdocs\views\site\index.php
但是,自从我开始使用 Yii2 以来,我已经能够使用 http://localhost/web/index.php?r=paramA/paramB
浏览我的网站,但我还没有编辑配置文件已经有一段时间了,所以我不知道为什么会发生这种情况.
However, I've been able to navigate my site with http://localhost/web/index.php?r=paramA/paramB
since I started using Yii2, and I haven't been editing configuration files for quite some time, so I don't know why this started happening.
一些可能有用的文件:
/web/index.php
(几乎没有编辑):
<?php
require_once __DIR__.'/../util/Tools.php';
// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');
(new yii\web\Application($config))->run();
controllers/SiteController.php
的一部分:
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
...
];
}
public function actionIndex()
{
return $this->render('index');
}
此外,在 config/web.php
中我没有 urlManager
组件.
Also, in config/web.php
I have no urlManager
component.
为什么会发生这种情况,我能做些什么?另外,由于我不知道在哪里搜索,我可能没有发布正确的代码进行分析,但我当然可以必要时添加代码.
Why is this happening and what can I do against it? Also, since I do not know where to search, I probably haven't posted the correct code to analyze, but of course I can add code when necessary.
此外,当我开始抛出一些UserExceptions
(显示一些无效的发布请求等)时,它就开始了.这立即显示了 /views/site/error.php
不存在的错误.我创建了那个 php 文件,但随后出现了上述问题.我删除了 /views/site/error.php
文件,但问题仍然存在.
Moreover, it started when I started throwing some UserExceptions
(To show some invalid post requests and such). That instantly showed an error that /views/site/error.php
did not exist. I created that php file, but then the problem described above occurred. I removed the /views/site/error.php
file, but the problem persists.
我猜这与 url 重写有关,但是在将 urlManager
组件添加到 /config/web.php
后没有任何反应(我尝试后将其删除)
I guess it has something to do with url rewriting, but nothing happened after adding an urlManager
component to /config/web.php
(which I removed after trying)
另外,在 \vendor\yiisoft\yii2\base\View.php
中有这样的代码:
Also, in \vendor\yiisoft\yii2\base\View.php
there's this code:
public function render($view, $params = [], $context = null)
{
$viewFile = $this->findViewFile($view, $context);
return $this->renderFile($viewFile, $params, $context);
}
$viewFile
返回找不到的路径(views\site\index.php
).
And $viewFile
returns the path which can't be found (views\site\index.php
).
此外,在 SiteController.php
中,$this->viewPath
是 C:\xampp\htdocs\views\site
(其中它不应该......?)
Also, in SiteController.php
, $this->viewPath
is C:\xampp\htdocs\views\site
(which it shouldn't..?)
堆栈跟踪:
1. in C:\xampp\htdocs\vendor\yiisoft\yii2\base\View.php at line 226
2. in C:\xampp\htdocs\vendor\yiisoft\yii2\base\View.php at line 149 – yii\base\View::renderFile('C:\xampp\htdocs\...', [], app\controllers\SiteController)
3. in C:\xampp\htdocs\vendor\yiisoft\yii2\base\Controller.php at line 371 – yii\base\View::render('index', [], app\controllers\SiteController)
4. in C:\xampp\htdocs\controllers\SiteController.php at line 58 – yii\base\Controller::render('index')
5. app\controllers\SiteController::actionIndex()
6. in C:\xampp\htdocs\vendor\yiisoft\yii2\base\InlineAction.php at line 55 – call_user_func_array([app\controllers\SiteController, 'actionIndex'], [])
7. in C:\xampp\htdocs\vendor\yiisoft\yii2\base\Controller.php at line 151 – yii\base\InlineAction::runWithParams(['r' => 'site/index'])
8. in C:\xampp\htdocs\vendor\yiisoft\yii2\base\Module.php at line 455 – yii\base\Controller::runAction('index', ['r' => 'site/index'])
9. in C:\xampp\htdocs\vendor\yiisoft\yii2\web\Application.php at line 84 – yii\base\Module::runAction('site/index', ['r' => 'site/index'])
10. in C:\xampp\htdocs\vendor\yiisoft\yii2\base\Application.php at line 375 – yii\web\Application::handleRequest(yii\web\Request)
11. in C:\xampp\htdocs\web\index.php at line 16 – yii\base\Application::run()
正如您现在可能已经看到的,我的 DOCUMENT_ROOT
是 C:\xampp\htdocs
,我使用的是基本模板
as you might have seen by now, my DOCUMENT_ROOT
is C:\xampp\htdocs
, and I am using the basic template
config/web.php
config/web.php
<?php
defined('DOCUMENT_ROOT') or define('DOCUMENT_ROOT', $_SERVER['DOCUMENT_ROOT']."/");
require_once DOCUMENT_ROOT . "/util/Tools.php";
$params = require(__DIR__ . '/params.php');
$config = [
"modules" => [
"gridview" => [
"class" => '\kartik\grid\Module'
]
],
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'SOMERANDOMSTRING',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
'authManager' => [
'class' => 'app\components\MyPhpManager',
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
控制器\SiteController.php
controllers\SiteController.php
<?php
namespace app\controllers;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\UploadForm;
use app\models\User;
use app\models\Document;
use app\components\XmlParser;
class SiteController extends Controller
{
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
'upload'=>['post'],
'assign'=>['post'],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
public function actionIndex()
{
return $this->render('index');
}
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
}
return $this->render('login', [
'model' => $model,
]);
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
public function actionAbout()
{
return $this->render('about');
}
public function actionAssign() {
//custom method
}
public function actionUpload()
{
//custom method
}
public function beforeAction($action) {
Yii::$app->controller->enableCsrfValidation = !($action->id == 'upload');
return parent::beforeAction($action);
}
}
推荐答案
所以,默认情况下,当你在 site
控制器中调用 render('index')
时,Yii 将在文件夹 web/views/site
中查找名为 index.php
的文件.这包含您的 site/index
视图文件.它应该在您创建站点时就在那里,但如果您还没有得到它,那么它一定是在某个阶段被删除了.创建文件并放入您的视图代码,您应该就可以开始了.
So, by default, when you call render('index')
in the site
controller, Yii will look in the folder web/views/site
for a file called index.php
. This contains your site/index
view file. It should have been there when you created the site, but if you haven't got it then it must have been deleted at some stage. Create the file and put in your view code, and you should be good to go.
这篇关于Yii2 错误:视图文件不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!