访问yii2控制器方法时找不到对象

时间:2015-02-15 17:34:23

标签: 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/

不确定我在这里失踪了什么,请你告诉我这个问题吗?

感谢Adavance

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

1 个答案:

答案 0 :(得分:5)

确定。我明白。你不能使用.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