ews2001
ews2001

Reputation: 2187

How to call controllers action & view directly from module in Yii?

I'm building my first module in Yii (yet another CMS module) to learn more about Yii and its capabilities. The next step is I'd like to add a "DEV bar" to the top of all of my pages. In my layout I added the following:

<?php echo Yii::app()->getModule('cms')->toolbar(); ?>

In my CmsModule file, I added a function named toolbar() and I'm not sure where to go from here. Basically, I want to send my Page model to the CMS toolbar, then render the CMS toolbar view. My toolbar should have links to edit the current page meta and page content. In order to following best practices in Yii & MVC, how would this best be achieved? In CmsModule, would I get the PageModel and if so, how would I render a CMS view? I've tried using $this->render(), but I get errors:

 Using $this when not in object context

Upvotes: 0

Views: 2857

Answers (1)

Michael H&#228;rtl
Michael H&#228;rtl

Reputation: 8607

I'm not sure what you mean by "Page Model" as this is not a Yii term. So I can not really help you on that.

But I think you misunderstood what a module in Yii is: It's like a sub-application with its own controllers, views and even models. A module is only active, when you call an action from that module.

It makes not much sense to fetch a new instance of your module with getModule('cms') if that module is not active at all. What you rather want instead is probably a widget which you can include in your main layout. From that widget you would render that top bar menu with links to your CMS module. You can also put that widget into your module's component directory if you want to keep the CMS related code together.

Upvotes: 1

Related Questions