Calling A Controller Method From View


My module directory structure is as follows:


- ProjectModule

  - views

    - default

      - index.php

      - dialog.php

    - layouts

  - models

  - messages

  - controllers

    - DefaultController.php

  - components 

From index.php file, I can call a method (eg. getName()) in DefaultController.php when I use "$this->getName()".

From dialog.php file, I want to call a method (eg. getName()) in DefaultController.php using “$this->getName()” but it cannot work. It seems like the $this is not referenced to the DefaultController of ProjectModule. How can I call a method in DefaultController from dialog.php inside the ProjectModule’s module?

What happens when you try this?


Same result as what I used for "$this->getName(id);".

The dialog doesn’t pop-up but no error message is shown.

If I comment out the "Yii::app()->controller->getName(id);" or "$this->getName(id);", the dialog box can appear. So I believe the error lies in the calling of the method from the defaultcontroller or probably returning the value from defaultcontroller to the dialog view.

Is there any other solutions to it?

Um, what Roman meant was, I guess, check to see what controller you are in when you get error.

I think you are rendering dialog.php from a controller that is not DefaultController.

Thanks all! I’ve found my error already.

It’s because there are typo errors in the getName() method.