tags:

views:

84

answers:

5

I am working on creating my own very simple MVC and I am brainstorming ways to go from the controller to the view. Which involves sending variables from a class to just a plain old PHP page.

I am sure that this has been covered before, but I wanted to see what kind of ideas people could come up with.

//this file would be /controller/my_controller.php

class My_Controller{

   function someFunction(){

  $var = 'Hello World';
  //how do we get var to our view file in the document root?
  //cool_view.php

   }

}
+1  A: 

Some kind of hashtable is a good way to do that. Return your variables as association array which will fill all the gaps in your view.

ŁukaszW.pl
So you recommend to just return the variables in the controller. And then in the view say $vars = new my_controller(); and then use the appropriate function. That is a nice easy solution indeed.
Mike
A: 

Store your variables as a property in your controller object, then extract them when rendering

class My_Controller {

    protected $locals = array();

    function index() {
        $this->locals['var'] = 'Hello World';
    }

    protected function render() {
        ob_start();
        extract($this->locals);
        include 'YOUR_VIEW_FILE.php';
        return ob_get_clean();
    }
}

You can define those magic __get and __set methods to make it prettier

$this->var = 'test';
Sean Huber
Be really careful with `extract`, read carefully before use http://ru2.php.net/manual/en/function.extract.php
Kirzilla
+1  A: 

I'm also developing my own simple MVC and the most simple way to do it is ...

class My_Controller
{

   function someFunction() {
       $view_vars['name'] = 'John';
       $view = new View('template_filename.php', $view_vars);
   }

}

View class

class View
{
   public function __construct($template, $vars) {
       include($template);
   }
}

template_filename.php

Hello, <?php echo $vars['name'];?>

I highly recommend you to take a look at PHP Savant http://phpsavant.com/docs/

Kirzilla
A: 

I created my own MVC for the free PHP course I'm conducting for a handful of people wanting to get better at PHP.

By far the best way to do this is to use the Command + Factory pattern.

E.g.

interface ControllerCommand
{
    public function execute($action);
}

In each controller:

class UserController implements ControllerCommand
{
    public function execute($action)
    {
        if ($action == 'login')
        {
            $data['view_file'] = 'views/home.tpl.php';
        }
        else if ($action == 'edit_profile')
        {
            $data['view_file'] = 'views/profile.tpl.php';
            $data['registration_status'] = $this->editProfile();
        }

        return $data;
    }
}

From your main front controller:

$data = ControllerCommandFactory::execute($action);
if (!is_null($data)) { extract($data); }
/* We know the view_file is safe, since we explicitly set it above. */
require $view_file;

The point is that every Controllercommand class has an execute function and that returns its view and any data for that view.

For the complete MVC, you can access the open source app by emailing me at theodore[at]phpexperts.pro.

hopeseekr
+1  A: 

I'd checkout Zend_View and how it accomplished view rendering.

You can get the source of View and AbstractView on github - unfortunaly I don't find the current repository (in svn) that easy to browse.

Essentially the view variables are contained in a View object (which your controller would have access to), then the template (plain old php document) is rendered inside that object. That method allows the template access to $this.

It would be something like:

<?php
class View
{
  public function render()
  {
    ob_start();
    include($this->_viewTemplate); //the included file can now access $this
    return ob_get_clean();
  }
}
?>

So in your controller:

<?php
class Controller
{
  public function someAction()
  {
    $this->view->something = 'somevalue'; 
  }
}
?>

And your template:

<p><?php echo $this->something;?></p>

In my opinion this pattern allows you much flexibility with the view.

Tim Lytle