Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YII: Passing data to a widget from a controller?

Tags:

php

yii

I have a search page that passes data to the render function:

public function actionIndex() {

  $this->render(
    'searchResults', 
    array(
      'dataProvider' => $dataProvider,
      'searchQuery'  => $searchQuery,
    )
  );
}

The problem is, I also need to pass this data from here to a widget that appears in the sidebar. The widget is currently displayed in layout/main.php like so:

 <?php 
    $this->widget('searchSidebar', array(
      'id' => 'searchSidebar',
    )); 
 ?>

How would I go about passing data to this widet from the controller without having to redo the query again?

like image 601
coderama Avatar asked Oct 22 '22 15:10

coderama


1 Answers

dataProvider already has all data included with

$dataProvider->data
$dataProvider->getData()

To place it in the main layout you may create another variable in your Controller

class Controller extends CController
{
    public $data_exchange='';
    ...
}

It will be easy to manipulate everywhere in the code with $this->data_exchange similar to breadcrumbs in your main layout

$this->widget('searchSidebar', array(
    'id' => 'searchSidebar',
    'data' => $this->data_exchange 
    /* where $this refer to any class which extends Controller */
)); 

In your view code define your data as:

$this->data_exchange = $dataProvider->data
like image 110
nanocult Avatar answered Oct 27 '22 11:10

nanocult