Search code examples
phpcakephpcakephp-2.x

how to get data into view from controller in cakephp


$this->loadModel('Siteconfig');

$data=$this->Siteconfig->find('all');

foreach($data as $data)
{           
    $email=$data['Siteconfig']['email'];

    $companyname=$data['Siteconfig']['companyname'];

    $cfa=$data['Siteconfig']['cfa'];
}

Above is Controller

How to get cfa data in view using CAKEPHP?


Solution

  • In your controller method, you can use this

    $this->set('var_name_in_view', $cfa);  //You will find the variable $cfa as $var_name_in_view in your view(You have to use $var_name_in_view in view)
    

    You can also send multiple variables to your view at once

    $this->set(compact('cfa', 'another_variable'));  //You can acceess using $cfa and $another_variable
    

    Compact makes an array keeping the variable name as key and variable value as the value for the array key.