Controller in modular fashion

Hi I’m trying to implement controllers in modular fashion as described in

http://www.yiiframework.com/doc/guide/basics.controller#action

in myController I’ve written:




public function actions()

        {

                return array(

                        'view'=>'application.controllers.user.ViewAction',

  

                );

        }



so, inside protected/controllers/user/ViewAction.php I’ve written:


<?php

        /**

         * Displays a particular model.

         */

        public function actionView()

        {

                $model = $this->loadModel();

                $this->render('view',array(

                        'model'=>$model,

                ));

        }

but, when I run user/view PHP returns:

Parse error: syntax error, unexpected T_PUBLIC in […]/controllers/user/ViewAction.php on line 5

where line 5 is "public function" line. Any hint?

Ciao

Danilo

It works like this:




class ViewAction extends CAction

{


        public function run()

        {

                $model = $this->loadModel();

                $this->render('view',array(

                        'model'=>$model,

                ));

        }


}



Class name must be the same as the file name.

/Tommy

Thanks Y!! && tri, it (almost) works :) except for “ViewAction does not have a method named “loadModel”.”

of course, loadModel is a controller’s method, so ViewAction doesn’t inherit “loadModel” method from UserController?

tia

danilo

try in your action::run() method


$this->getController()->loadModel()

I dont know how familiar you are with php and programming in general, but usually my first step to figuring out these types of questions would be to open the relevant source file. In this case your extending CAction so open up /path/to/yii/web/actions/CAction.php This is a fairly simple class and the method you need jumps right out :)

journey4712

Journey thanks for the solution, but especially thanks for tip to solve question!

Actually I’m a not a PHP OOP expert, but I promise to follow your suggested solution for future problems :)