In the controller Things y have a public method 'funnyThings'
class ThingsController extends CController
{
//...
public function funnyThings()
{
//... obtains data from his model
return array(/*the data in form 'key'=>'value'*/);
}
//...
}
Now in PersonController I want to do something like that, but I can't
class PersonController extends CController
{
//...
public function actionShow ()
{
// obtains the person data in $person
$things = ThingsController::funnyThings(); // This is what not works
$this->render('show',array('person'=>$person,'things'=>$things);
}
//...
}
It does not work… the following error apears:
YiiBase::include(ThingsController.php) [<a href='yiibase.include'>yiibase.include</a>]: failed to open stream: No such file or directory
You need to import/include the class file before referencing the class.
You should also consider refactoring your code by putting the commonly used method into a separate class. It is not so nice to call a controller method in another controller.
Yes. Well, I will explain further so you can show me the correct way to do…
Person has many things.
Then I have two models (AR) Person and Class.
The crud command creates views for both.
I want to use the admin view of Things as part of the show view of Person. But the admin view of Things need some parameters ($sort, $pages, $thingsList), and because exist a method (actionAdmin) that already does that, I just want to use that method (to say better I make a method named funnyThings that it's exactly equal to actionAdmin, except that actionAdmin renders a view and funnyThings returns an array, —note: the common code between these two methods is already in another private function in ThingsController, so I no repeat the same twice).
May be exists another way to do what i want, but sincerely I don't know how.
Does the private method reference any member variable of the controller instance? If not, you may declare it as a public static method. And then you can call this method from anywhere. A better approach would be creating a new class and move this method into the new class.
If that's the case, you may create an action class with a property to allow configuring which view to render. Then you can reuse this action class in both controllers.
Ok, I will try in this direction now, but… Where in the skeleton I must put that class? In components? in that case, I must declare a component? or in the actions method of my controllers?