1
votes

I have written a separate action class which is called from a controller like this:

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) protected/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));
    }

}

when I call url http://localhost/test/soccer/index I get a fatal error Cannot redeclare class IndexAction in C:\wamp\www\test\protected\actions\soccer\IndexAction.php on line 25

Can any body help me why this error occurs ?

1
why are you writing a separate action class ? - Criesto
Instead of having all the actions in a controller I tried to break down to different classes. - Gowthamr.cs
you could just create a separate module if you want or multiple controllers, if you think a single controller has too many actions. - Criesto
Yes, but I was wondering why this solution is not working :) - Gowthamr.cs

1 Answers

1
votes

From the Yii docs:

To define a new action class, do the following:

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

In order for the controller to be aware of this action, we override the actions() method of our controller class:

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

In the above, we use the path alias application.controllers.post.UpdateAction to specify that the action class file is protected/controllers/post/UpdateAction.php.

So, 'index' => 'application\actions\soccer\IndexAction', this is the cause of the error, try changing this to: 'index' => 'application.controllers.soccer.IndexAction', or whatever your path is.