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

标签 php yii yii2 yii2-user

嗨,我是 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/

相关文章:

javascript - Yii2 中的 Route 和 Post 参数与 ajax

listview - Yii2 扩展/自定义 GridView

error-handling - 如何在生产系统中获取Yii2格式的错误消息?

php - 如何创建 PHP 多维数组

php - 在 php 中,即使在简短的条件语句中也应该使用大括号吗?

php - cakephp 检查重复行

php - 静态页面的 htaccess 条件 https

在 Symfony 1 中 PHP 警告 "Warning: ob_start(): function ' '未找到或无效的函数名称?

javascript - Ajax 提交错误

php - YII - 当你可以在 Save() 函数之前编码时为什么要使用 beforeSave()