0

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
2
  • I'm not sure what your base question is here (I have no experience with writing modules in Yii), but to resolve the error listed try using Yii::app()->controller->render() Commented Apr 18, 2013 at 16:47
  • Thank you for the comment about the error, it doesn't answer my question, but helps. My base question is about the best way to display a view from a specific controller within a module? Commented Apr 18, 2013 at 17:11

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Okay, I think I see now where to go next because of your suggestions, let me take a stab at it and report back.
I updated my layout with this code: <?php $this->widget('cms.components.Toolbar', array()); ?> and added a Toolbar.php to cms/components. Toolbar extends CWidget and I've added my HTML to the run() function. I hope this is what you were explaining to do...now how do I pass my model $page to the Toolbar since $page isn't available in my layout?
Sounds good. You can add a getter method getPage() to your controller where you return the current page model (whatever that is and however you retrieve it - probably from some $_GET parameter). Then you can access it through $this->page in your layout file. You then can pass it as parameter to your widget $this->widget(...,array('model'=>$this->page)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.