【问题标题】:PHP Yii Framework - Controller works incorrectly?PHP Yii 框架 - 控制器工作不正确?
【发布时间】:2016-05-11 22:16:34
【问题描述】:

我有一个使用简单方法的控制器:

<?php

class SiteController extends CController
{
    /**
     * Declares class-based actions.
     */
    public function actionIndex()
    {
        echo 'Zostala wywołana akcja Index';
    }

    public function actionPokaz()
    {
        echo 'Zostala wywołana akcja Pokaz';
    }

}

现在当我去: http://localhost/yii/test/site 它应该运行一个 actionIndex 方法,但是我得到“找不到对象”信息。这是为什么呢?

更重要的是,我正在按照我正在阅读的书(今天才开始)做所有事情,书上说它应该有效,但它没有。我的项目文件夹当然是 yii/test,位于 XAMPP htdocs。

错误如下:

Nie znaleziono obiektu! (Object not found!)

Nie znaleziono żądanego URL-a na tym serwerze. Odnośnik na referującej stronie wydaje się być nieprawidłowy lub nieaktualny. Poinformuj autora tej strony o problemie. (Desired URL was not found on this server ...)

Jeśli myślisz, że jest to błąd tego serwera, skontaktuj się z administratorem.

Error 404

localhost
Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.15

这是波兰语。但是我把翻译放在括号里。

我的 main.php:

<?php

// uncomment the following to define a path alias
// Yii::setPathOfAlias('local','path/to/local-folder');

// This is the main Web application configuration. Any writable
// CWebApplication properties can be configured here.
return array(
    'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
    'name'=>'My Web Application',

    // preloading 'log' component
    'preload'=>array('log'),

    // autoloading model and component classes
    'import'=>array(
        'application.models.*',
        'application.components.*',
    ),

    'modules'=>array(
        // uncomment the following to enable the Gii tool
        /*
        'gii'=>array(
            'class'=>'system.gii.GiiModule',
            'password'=>'Enter Your Password Here',
            // If removed, Gii defaults to localhost only. Edit carefully to taste.
            'ipFilters'=>array('127.0.0.1','::1'),
        ),
        */
    ),

    // application components
    'components'=>array(

        'user'=>array(
            // enable cookie-based authentication
            'allowAutoLogin'=>true,
        ),

        // uncomment the following to enable URLs in path-format

        'urlManager'=>array(
            'urlFormat'=>'path',

            'rules'=>array(
                '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                'site/policz/<liczba1:\d+>/<liczba2:\d+>'=>'site/policz/',

            ),
        ),


        // database settings are configured in database.php
        'db'=>require(dirname(__FILE__).'/database.php'),

        'errorHandler'=>array(
            // use 'site/error' action to display errors
            'errorAction'=>YII_DEBUG ? null : 'site/error',
        ),

        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array(
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning',
                ),
                // uncomment the following to show log messages on web pages
                /*
                array(
                    'class'=>'CWebLogRoute',
                ),
                */
            ),
        ),

    ),

    // application-level parameters that can be accessed
    // using Yii::app()->params['paramName']
    'params'=>array(
        // this is used in contact page
        'adminEmail'=>'webmaster@example.com',
    ),
);

【问题讨论】:

标签: php web yii controller


【解决方案1】:

如果您使用如下 url 格式,您可以从浏览器访问您的控制器操作

// for index action
localhost/yii/test/index.php?r=site/index

// since index action is default for any controller, following url also return index action
localhost/yii/test/index.php?r=site

// to access Pokaz action put this url in the browser
localhost/yii/test/index.php?r=site/pokaz

为什么喜欢这样?问题出在哪里?

您需要了解以下教程。请花点时间阅读本教程,这是您在每个 Yii 1 项目中都需要的内容,即使您要将应用程序部署到托管服务器也是如此。如果您对教程中的任何内容不了解,请在此处的评论部分提问

http://www.yiiframework.com/doc/guide/1.1/en/topics.url

还要确保 Apache 模块 mod_rewrite 与您的 XAMPP 一起安装

【讨论】:

  • 谢谢!那真的很有帮助。很好,但是为什么这本书没有告诉我这些事情……这本书显然是错误的。再次感谢,我现在会处理的。
  • 很高兴它有效。我提供的指南链接还有其他部分。我相信这是学习 Yii 的绝佳资源。你也可以探索这些。如果这个答案是对的,那就把它标记为正确答案,这将有助于其他人快速找到类似的解决方案并跳入正确的答案
【解决方案2】:

您所要做的就是首先启用 url 管理器。因此,在您的测试文件夹中添加一个名为 .htaccess 的新文件(不受保护)。

并将下面的代码添加到您的 .htaccess 文件中

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

并像这样将showScriptName'=&gt;false, 添加到config/main.php 的urlManager 中..

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName'=>false,
    'rules'=>array(
            '<controller:\w+>/<id:\d+>'=>'<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
            '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
    ),

然后您可以使用以下链接打开您的网站 http://localhost/yii/test/site

始终从CController 扩展,如果您只想使用Controller,那么您必须在protected/components 中创建一个带有Controller extends CController 的自定义基控制器类。然后,您的应用程序的所有控制器类都应该从这个基类扩展。

class SiteController extends CController
{

// your functions here

}

【讨论】:

  • 我尝试扩展控制器,但它也不起作用 - 忘了提到这一点。同样在我正在阅读的书中,他们说扩展 CController 没有控制器。真的,不知道为什么它不起作用。
  • 是的,我知道我会在这个链接下获得 actionIndex。在书中他们说我应该去 yii/test/site/pokaz,它应该可以工作,但它没有。好吧,我注意到如果我去localhost/yii/test/index.php/site/pokaz 我会得到actionPokaz,但为什么它会这样工作而不是他们在书中所说的方式,你能解释一下吗?
  • 我想在你的 config/main.php 文件 'showScriptName'=&gt;false, 的 url manager 区域中添加此代码。
  • 在添加 'showScriptName'=>false 后仍然无法按照我想要的方式工作,
  • 在你的 actionIndex() 或 actionPokaz() 中告诉我,你是如何渲染页面的?从 main.php 发布您的索引或 pokaz 以及您的 urlmanager 的完整代码
【解决方案3】:

你应该扩展 Controller 而不是 CController

   <?php

  class SiteController extends Controller
  {
      /**
       * Declares class-based actions.
       */
      public function actionIndex()
      {
          echo 'Zostala wywołana akcja Index';
      }

      public function actionPokaz()
      {
          echo 'Zostala wywołana akcja Pokaz';
      }

  }

【讨论】:

  • 我尝试扩展 Controller,但效果不佳 - 只是忘了提及。
  • 你使用的是 Yii1 还是 Yii2 ?
  • 是Yii1,因为书和Yii1有关。
  • 总的来说,如果我转到项目中存在的目录或文件,我会得到 actionIndex()。但是,如果我选择转到不存在的目录,我也应该得到 actionIndex,因为它是默认的,对吗?真的,我读了这本书的 5 页,卡住了,不知道该怎么走,还有更多的章节基于这个控制器,所以不知道该怎么办。
猜你喜欢
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
相关资源
最近更新 更多