【问题标题】:Cannot redeclare class error in Yii无法在 Yii 中重新声明类错误
【发布时间】:2015-12-04 05:39:34
【问题描述】:

我已经编写了一个单独的动作类,它是从这样的控制器调用的:

1) protected/controllers/SoccerController.php

class SoccerController extends Controller
{
    public function actions() {
        return array (
            'index' => 'application\actions\soccer\IndexAction',
            'teamlist' => 'application\actions\soccer\TeamListAction'
        );
    }
}

2) 受保护/actions/soccer/IndexAction.php

class IndexAction extends CAction
{
    public $viewFile = 'teamoverview';

    /**
     * executes the action.
     */
    public function run()
    {
        $team = new SoccerTeam();
    $this->controller->render($this->viewFile, array('team'=>$team));
    }

}

当我调用 url http://localhost/test/soccer/index 时出现致命错误 Cannot redeclare class IndexAction in C:\wamp\www\test\protected\actions\soccer\IndexAction.php on line 25

任何机构可以帮助我为什么会发生此错误?

【问题讨论】:

  • 你为什么要写一个单独的动作类?
  • 我没有将所有动作都放在控制器中,而是尝试分解为不同的类。
  • 如果你想创建一个单独的模块,或者如果你认为一个控制器有太多动作,你可以创建多个控制器。
  • 是的,但我想知道为什么这个解决方案不起作用:)

标签: php yii


【解决方案1】:

来自Yii docs

要定义新的动作类,请执行以下操作:

class UpdateAction extends CAction
{
    public function run()
    {
        // place the action logic here
    }
}

为了让控制器知道这个动作,我们重写 我们控制器类的actions() 方法:

class PostController extends CController
{
    public function actions()
    {
        return array(
            'edit'=>'application.controllers.post.UpdateAction',
        );
    }
}

在上面,我们使用路径别名 application.controllers.post.UpdateAction 指定动作 类文件是protected/controllers/post/UpdateAction.php

所以,'index' => 'application\actions\soccer\IndexAction', 这是错误的原因,请尝试将其更改为: 'index' => 'application.controllers.soccer.IndexAction', 或任何你的路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 2010-10-17
    相关资源
    最近更新 更多