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
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.
A few files which might be useful:
/web/index.php
(barely edited):
<?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();
part of controllers/SiteController.php
:
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
...
];
}
public function actionIndex()
{
return $this->render('index');
}
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.
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.
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)
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);
}
And $viewFile
returns the path which can't be found (views\site\index.php
).
Also, in SiteController.php
, $this->viewPath
is C:\xampp\htdocs\views\site
(which it shouldn't..?)
The stack trace:
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()
Edit:
as you might have seen by now, my DOCUMENT_ROOT
is C:\xampp\htdocs
, and I am using the basic template
Edit2:
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;
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With