gpt4 book ai didi

php - 访问 yii2 Controller 方法时找不到对象

转载 作者:可可西里 更新时间:2023-11-01 00:43:04 25 4
gpt4 key购买 nike

嗨,我是 YII 2 框架的新手,

我目前正在学习以下教程 http://www.yiiframework.com/wiki/490/creating-a-simple-crud-app-with-yii2-revised-12-20-2013/

一切正常,但是当我在 SiteController.php 中创建一个函数时

 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();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}

当我按如下方式从浏览器访问它时,

http://localhost/basic/web/site/login/

我得到了

未找到对象! 在我的浏览器中,但我可以访问 SiteController.php 索引函数,如下所示 http://localhost/basic/web/

不确定我在这里遗漏了什么,你能告诉我这个问题吗?

提前致谢

编辑:出于调试目的,我将 die 语句放在 \basic\web\index.php 中,显然它也没有命中该文件

最佳答案

好的。我明白。您没有使用 .htaccess。请将此 .htaccess 放在 web 文件夹中。您需要检查 Apache 模块 mod_rewrite 现在是否可用。

#Options +FollowSymLinks
#IndexIgnore */*

#RewriteEngine on

# if a directory or a file exists, use it directly
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php
#RewriteRule . index.php


# use mod_rewrite for pretty URL support
RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php

# ...other settings...

查看更多信息 https://github.com/yiisoft/yii2/blob/master/docs/guide/start-installation.md#recommended-apache-configuration-

components 中的 urlManager 类似 https://yadi.sk/i/TIKuhYPHehMJq

'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'enableStrictParsing' => true,
'rules' => [
'<_c:[\w\-]+>' => '<_c>/index',
'<_c:[\w\-]+>/<_a:[\w\-]+>' => '<_c>/<_a>',
'<_c:[\w\-]+>/<_a:[\w\-]+>/<id:\d+>' => '<_c>/<_a>',
],
],

这是工作 - https://yadi.sk/i/7iOzHBm1ehMFE

关于php - 访问 yii2 Controller 方法时找不到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28528960/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com