我已經使用了Yii2幾個星期了,並獲得了它的竅門。然而,今天,是有原因的,我不知道,Yii中一直路由我錯誤的頁面,從而導致錯誤,因爲網頁還沒有被發現:Yii2錯誤:視圖文件不存在
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
不過,我已經能夠瀏覽我的網站http://localhost/web/index.php?r=paramA/paramB
自從我開始使用Yii2以來,我一直沒有編輯配置文件,所以我不知道爲什麼會發生這種情況。
幾個文件,這可能是有用的:
/web/index.php
(勉強編輯):的controllers/SiteController.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();
部分:
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
...
];
}
public function actionIndex()
{
return $this->render('index');
}
此外,在config/web.php
我沒有urlManager
組件。
爲什麼會發生這種情況,我該怎麼辦?此外,由於我不知道在哪裏搜索,我可能沒有發佈正確的代碼來分析,但我當然可以在必要時添加代碼。
此外,它開始時,我開始投擲一些UserExceptions
(要顯示一些無效的發佈請求等)。這即刻顯示一個錯誤,/views/site/error.php
不存在。我創建了這個php文件,但之後發生了上述問題。我刪除了/views/site/error.php
文件,但問題仍然存在。
我猜它是與URL重寫,但添加urlManager
組件/config/web.php
(我嘗試後刪除)
後也沒有動靜,在\vendor\yiisoft\yii2\base\View.php
有這樣的代碼:
public function render($view, $params = [], $context = null)
{
$viewFile = $this->findViewFile($view, $context);
return $this->renderFile($viewFile, $params, $context);
}
而$viewFile
返回無法找到的路徑(views\site\index.php
)。
此外,在SiteController.php
,$this->viewPath
是C:\xampp\htdocs\views\site
(它不應該..?)
堆棧跟蹤:
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
,而我正在使用基本模板
編輯2:
配置/ 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
<?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);
}
}
您使用基本或高級模板? – scaisEdge
我有基本模板 – stealthjong
你可以嘗試登錄嗎? – scaisEdge