Yii Framework Forum: How To Add Sidebar In All Controller Actions - Yii Framework Forum

Jump to content

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

How To Add Sidebar In All Controller Actions Rate Topic: -----

#1 User is offline   auzadventure 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 20-August 15
  • Location:Singapore

Posted 13 April 2017 - 11:10 PM

So you want to add a sidebar for all views
in a certain controller example "user".

This has been annoying me for months, so I've decided to write a tutorial how to do it simple in Yii2.

1. Update Your Layout File To Handle 2 Columns on bootstrap. Add

$this->params['sideBar']!='';



<?php $this->beginContent('@app/views/layouts/main.php'); ?>

<div class="row">
   <div class="col-md-3">
    <div class="pg-sidebar">          
      <?php 
		if(@$this->params['sideBar']!='') 
			echo $this->params['sideBar'];  
	  
	  ?>

      <?= $this->blocks['toolbar']; ?>
    </div>      
  </div>
  <div class="col-md-9">
    <?= $content; ?>
  </div>
</div>

<?php $this->endContent(); ?>



2. Update your action to use this new layout. Test it to see hello.

	public function actionAccount() {
		$this->layout = "/column2";    <----- 
		Yii::$app->view->params['sideBar'] = 'hello';
		
		return $this->render('account');
	}


3. Using the special variable of "Yii::$app->view->params['sideBar']"
we can now set it to anything.
Use a simple render to change it to whatever sidebar you want. Put True behind to echo the return as a string.

In Controller
Yii::$app->view->params['sideBar'] = $this->renderPartial('partials/_profile', ['name'=>'value'],true);



4. If you want it to appear for all action, just add it to before action or in the module section.
Questions are the Answer.

Wesvault - Premium Economy Software
Yii2 Framework Specialist
http://www.wesvault.com

Singapore
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