我已经使用 Yii2 几个星期了,并且掌握了它的窍门。然而,今天,由于我不知道的原因,Yii 将我路由到错误的页面,导致错误,因为没有找到网页:
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 导航我的站点,但我还没有编辑配置文件已经有一段时间了,所以我不知道为什么会这样。
一些可能有用的文件:
/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 组件。
为什么会发生这种情况,我该如何应对?另外,由于我不知道去哪里搜索,我可能没有发布正确的代码来分析,但我当然可以必要时添加代码。
此外,当我开始抛出一些 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;
Controller \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 Controller 中调用 render('index') 时,Yii 将在文件夹 web/views/site 中查找 用于名为 index.php 的文件。这包含您的 site/index View 文件。它应该在您创建网站时就在那里,但如果您没有得到它,那么它一定是在某个阶段被删除了。创建文件并放入您的 View 代码,您应该可以开始了。
关于php - Yii2错误: The view file does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34396976/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]
尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot