YII: Transferring data to a widget from a controller?

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

public function actionIndex() {

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

The problem is that I also need to transfer this data from here to the widget that appears in the sidebar. The widget is currently displayed in layout / main.php as follows:

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

How can I proceed to transfer data to this widget from the controller without re-requesting the request?

+5
source share
1 answer

dataProvider already has all the data included in

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

To put it in the main layout, you can create another variable in your controller

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

It’s easy to manipulate everywhere in your code using the $this->data_exchangesame breadcrumbsas 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
+3
source

All Articles