3
votes

How do PHP programs pass values between model, view, and controller pages? For example, if a controller had an array, how does it pass that to the view?

EDIT:

Thank your answers. I see a couple of them stating the components are in the same page, but when I look at something like CodeIgniter, I see three separate PHP pages for model, view, and controller.

6

6 Answers

6
votes

Usually your controller will create a view object and when it creates that view object it passes the information.

<?php

class Controller {
    public function __construct() {
        $arr = array("a","b","c");

        $myView = new View($arr);
    }
}

class View {

    private $content;

    public function __construct($content) {
        $this->content = $content;
        include_once('myPage.inc.html');
    }
}


?>
2
votes

CodeIgniter, like most PHP MVC frameworks, comes with a View "engine". In otherwords, there's a class in the framework responsible for loading and transferring data from your controller to the view.

Specific to CodeIgniter, those calls look like this:

$data = array(
           'title' => 'My Title',
           'heading' => 'My Heading',
           'message' => 'My Message'
      );

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

Your view would then be a separate php page, in this case blogview.php. It may look like this:

<html>
   <head><title><?= $title ?></title></head>
   <body>
      <h2><?= $heading ?></h2>
      <p><?= $message ?></p>
   </body>
</html>

The part responsible for transferring the data between the controller and the view is the View engine (or class) internal to CodeIgniter. It takes that array from the controller and deserializes it for the view.

So why two separate files? The MVC paradigm is such that, by design, you want your business logic separated from your UI. You don't need to incorporate the Model part of MVC if you aren't doing much with a database. But it's there when you want to further separate the data access portions of your code from your business logic (the stuff that manipulates the data for the user). In that case, you've got your 3 separate files; model, controller, view.

So no, the components aren't all in the same page, though they certainly could be. But by structuring your program using MVC, you separate the operations of your application cleanly and neatly. That way, you can work in one layer at a time without effecting the other layers. Your CSS guy could be making the site look pretty while you figure out why the user can't log in, for example.

1
votes

I hope Johnny has long ago found the answer that I'm searching for now, but I can supply an insight on how to do it unsatisfactorily! You can pass from one info from one file (say a 'view' component that collects data input by a site user) to another file (say a 'model' component that checks/validates the form data and either sends it back to the form for revision or stores it in a database). Form values can be sent via the POST or GET arrays and the 'action' attribute determines which file receives the data e.g.

<form action = 'form-exec.php' method = 'POST'>
---some set of  possible user inputs goes here
<input name='submit' type='submit' value='Go'>
</form>

After processing, you may well want to return some data to the form - such as the user input values that need to be amended, or a success message. You can store that data in session variables and recover it from those variables on the 'view' page. So, how do you get back to the 'view' page? Now, I'm here because I have seen this used and have used it myself but was pretty horrified to read that, apparently, the transfer goes via a dog-slow internet http request - even when the transfer is to a file in the same directory on the server! Okay, so the UNSATISFACTORY way to get back to your 'view' component is a couple of PHP lines like this:

header("location: theinputform.php");
    exit;

It does work, but there must be a better way, surely? I have seen a 'rant' from someone saying that you should just use an include, but I can't understand how to do the reasonably everyday thing of going back to the top of a page and recreating it according to the new conditions. For example, if I just logged a User in I don't want to be showing an invitation to login this time around. Includes are the no-brainer for conditionally ADDING something to a page, but I haven't found a better way to simply return to the top than the above. Does anyone have any insight or knowledge on this? As the OP states, Johnny and I want to do the same as a MVC but are not using a MVC framework of any kind. There are several books and tutorials out there that simply use the above 'headers' method, btw.

Okay, edit: You get from a form (i.e. a 'view' page) to the form processing using the form's 'action' attribute, as above. If the 'model' page outputs no html and just 'does things', you can simply include the 'view' page that you wish to go to (or return to) on completion. I think so, anyway! Just about to try that...

1
votes

I struggled with this question myself. I had returned to php programming after nearly a decade. I ran into couple of issues deploying PHP MVC frameworks into an experimental server so I ended up building a simple MVC variant myself.

In my simple design (front) controller interacts with the Model to populate a payload object and passes it to the view. Each view is implemented in a separate PHP file so controller uses include, to "load" the desired view. The view then can access the data via the designated name ($dataId) for the payload object.

class Controller {
        public $model;
        ...
        public function display($viewId,$dataId,$data)
        {
          $url = $this->getViewURL($viewId);
          $this->loadView($url,$dataId,$data);              
        }

        public function getViewURL($key)
        {
            $url = 'view/list_view.php';

            if($key == 'create')
            {
                $url = 'view/create_view.php';
            }
            else if($key == 'delete')
            {
                $url = 'view/delete_view.php';
            }


            return $url;            
        }

        public function loadView($url,$variable_name, $data)
        {
            $$variable_name = $data;
            include $url;
        }
}
0
votes

model, view, and controller are not separate pages but one page.
So, no need to "pass" anything

-2
votes

u can use a template engine like smarty to seperate views from controllers and models.

and pass variables to your template using assign method of smarty for example.