tags:

views:

97

answers:

4

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.

A: 

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

Col. Shrapnel
why do they appear to be separate pages? For example, CodeIgniter, at lease to me, clearly has three php pages.
johnny
the same reason you separate any program into several files. It's for organization.
Simurr
@simuri I'm saying the the Col. said they are not separate pages and I'm saying they are because I can see three separate pages and in fact edit three pages. I'm not sure you understand what I meant.
johnny
@johnny these pages being included into each other. That makes them one page.
Col. Shrapnel
@col. so somewhere there is an explicity "include" or "include_once"?
johnny
@johnny exactly
Col. Shrapnel
A: 

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.

rahim asgari
why did you get a down vote?
johnny
@johnny i dont know my self! ;)
rahim asgari
Downvote: 1. Smarty is one of the most crappiest template engine ever made. 2. Smarty has nothing to do with View from MVC, cause it's only a template engine.
Crozin
so you cannot use it to separate views from controllers and models?
johnny
@Crozin read this. http://ocaoimh.ie/using-smarty-to-implement-the-mvc-design-pattern-in-php/ there are thousands of this kinds of articles on the net!
rahim asgari
Seems kinda weird to downvote something just because you have a negative opinion about it lol
Ascherer
@Johhny: View **is not** a template. View might (not) use a template. @Ascherer: Downvote was for 2nd point. @rahim asgari: Smarty has been used as a "tool for the View", but Smarty cannot handle any of typical view task as it is only a template engine.
Crozin
+3  A: 

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');
    }
}


?>
cdnicoll
why are they usually in different pages with mvc frameworks?
johnny
When you do "include_once" above and you run the php above, am I correct that is including the .html in the current php page above? Why not just have it all in one page?
johnny
PHP isn't like c++ or c# in that you don't include or use namespaces (PHP 5.3 actually has namespaces) When you use include_once() you actually append that script to the page. The main reason you break your scripts apart is for programming logic.
cdnicoll
+1  A: 

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.

villecoder
"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." But how does it do that? How would I do the same on a very basic level from scratch?
johnny
Use the php function `extract`. That will turn the array's keys into individual variables. See http://php.net/manual/en/function.extract.php.
villecoder