1

I am not actually asking the syntax and the how do it. That, I got it from: codeigniter: pass array from controller to view

I need to ask how actually it works and what the reason for the process.

I have a controller class with func..

public function index()
{
    //echo 'hello world';
    $this->load->model('Site_model');

    var_dump($this->Site_model->getAll()); // just to troubleshoot

    $data['a'] = $this->Site_model->getAll();
    var_dump($data); // just to troubleshoot

    $this->load->view('home', $data);
}

When I try to access the $data array directly in view

It throws error and need to be accessed instead as <?php var_dump($a); ?>

Why can't we directly use this

public function index()
{
    //echo 'hello world';
    $this->load->model('Site_model');

    var_dump($this->Site_model->getAll()); // just to troubleshoot

    $data = $this->Site_model->getAll();
    var_dump($data); // just to troubleshoot

    $this->load->view('home', $data);
}

and access the array as <?php var_dump($data); ?> directly in view page

Community
  • 1
  • 1
  • You should actually ask , what is the purpose of using MVC – Shehzad Bilal Aug 18 '14 at 12:14
  • Why not the second option .. why extracting.. that could also fulfil that criteria? –  Aug 18 '14 at 12:18
  • @ShehzadBilal , if you actually learn some basics of MVC, you will realize that controllers should never pass information to view. http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller – tereško Aug 18 '14 at 13:00

2 Answers2

3

The procees that is being used behind is the simple one. It just uses a php function.

extract($data);

this lets all the keys to be used as variables on view.

See Docs here

Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103
  • 2
    @stackup nothing is wrong with it, feel free to use that in any framework you create. Ellis labs decided to go with this format however, probably because `echo $title` looks neater than echo `$data['title']` – Steve Aug 18 '14 at 12:20
  • Out of curisity Actually I tried it .. it was throwing error Undefined variable: data –  Aug 18 '14 at 12:24
  • So this particular syntax is used by ellis .. I haven't tried another MVC.. Thanks any ways.. –  Aug 18 '14 at 12:25
  • 1
    Also see [this](http://stackoverflow.com/questions/13171832/using-numeric-indixes-to-pass-data-from-controller-to-view) question. It will be helpful. – Muhammad Raheel Aug 18 '14 at 12:27
0

There is a number of reasons to use extract() here:

  1. It allows us to use unnamed arrays in controllers, like: $this->load->view('home', array('foo' => $bar));;
  2. it's the easiest way to keep the same non-global variable names when passed to another function.
  • can you clarify your first point.. Like how to access that array in view and what is its application –  Aug 18 '14 at 12:40
  • @stackup You may consider this array as a wrapper for your variables to preserve their names. For example, in controller: `$this->load->view('home', array('foo' => 5));` will become `$foo` with value of `5` in view and so on. – Okwinza Aug 18 '14 at 12:54