【问题标题】:Object not found when accessing yii2 controller methods访问 yii2 控制器方法时找不到对象
【发布时间】:2015-02-16 13:19:32
【问题描述】:

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

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

提前感谢

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

【问题讨论】:

  • 可能是 SiteController 是默认控制器。尝试在浏览器中打开http://localhost/basic/web/login/
  • 感谢您的意见,但我已经尝试过它不起作用。

标签: php yii yii2 yii2-user


【解决方案1】:

好的。我明白。你不使用.htaccess。请将此.htaccess 放入网络文件夹。您需要检查一下 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-中查看更多信息

urlManager 中的components 一样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

【讨论】:

  • 谢谢你,你救了一天,不知道为什么官方没有提到这件事。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 2014-04-10
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多