Yii Framework Forum: Controller in modular fashion - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Controller in modular fashion ehm... it doesn't work Rate Topic: -----

#1 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 27 February 2010 - 01:48 PM

Hi I'm trying to implement controllers in modular fashion as described in
http://www.yiiframew...ntroller#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
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 27 February 2010 - 02:03 PM

It works like this:

class ViewAction extends CAction
{

        public function run()
        {
                $model = $this->loadModel();
                $this->render('view',array(
                        'model'=>$model,
                ));
        }

}

0

#3 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 27 February 2010 - 02:04 PM

View Postzitter, on 27 February 2010 - 01:48 PM, said:

Hi I'm trying to implement controllers in modular fashion as described in
http://www.yiiframew...ntroller#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


Class name must be the same as the file name.

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#4 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 27 February 2010 - 02:12 PM

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
0

#5 User is offline   journey4712 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 02-July 09

Posted 27 February 2010 - 02:18 PM

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
0

#6 User is offline   zitter 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 140
  • Joined: 14-July 09

Posted 27 February 2010 - 02:22 PM

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 :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users