so, i’ve been pretty much a zend framework addict, ever since i coded my first bootstrap.  thinking back to that countless hours trying to understand the beast that is zf, ahhh, what fond memories…  and lately i’ve also been playing around a bit with kohana, which is another web based mvc framework, but definitely a bit more lightweight and easier to use than others.

there is however a bit of an issue with web based mvc frameworks, and in my opinion a large freaking elephant that no one seems to talk about.  how can i call a controller from within a view and assign an action to it – and is it correct to do so?

and before the quiet whispers start, i’d like to note that this does not necessarily go against mvc principles.  if you think about it – the whole point of mvc is to seperate business logic, application logic and the view (presentation logic) – and i’ve confirmed this with a couple of conversations with other developers.  but on the other hand, if you feel that i have overstepped a boundary – please do let me know if there is a better, or more correct way of doing this.

now lets give a practical problem:

i have a view template, lets call it home, which consists out of a header, navigation, content and footer.  now my content is pretty much controlled by the controller and whatever the view calls to display.  but say for example something you wish to partially include a login form in the top header.

how do i actually use a controller to change the way the view behaves, without building the logic into all the controllers which i access.  now in order to overcome this – we can attach a controller, lets call it log controller, to the view.  this controller will have the business logic to decide whether or not a login or a logout form must be generated.

now the obvious would be to include a partial view, but then the issue arrives where you let your view start making logic decisions, instead of presentation. on the other hand, you can use the current controller to help you decide this, but if you ask me, that might be a bit of overkill and lots of extra and duplicate coding.

the solution:

in zend framework, i found the solution to be a bit easier than in kohana, but still fun to figure out.

Zend Framework

zend framework:

okay, so i have my home template, which is controlled by my home controller.  on my home template in my header i have a form which would change depending on if the user is logged in or not, which i would like to be controlled by my log controller.

so, in my view i would do the following call:

<?php echo $this->action('log','generateForm') ?>

which would go to the log controller and call the method generateForm() – so in generate form i will be able to decide whether the user is logged in or not and assign the corresponding result to the view assigned to the controller, in the case above it would most likely be the generateForm view.

there you go – without having to overcomplicate things or duplicate code, you can assign controllers as part of your presentation logic with a simple call.  i have however not yet figured out how to pass variables to the action method you are calling, but as soon as i have an update i will let you know.

Kohana

kohana:

in kohana the task is  a bit more complicated – luckily i found in the underbelly of the kohana forums an user with the same problem – which came up with the following solution -

<?php
class Dispatch_Core{

protected $controller;

public static function controller($controller)
{
$controller_file=strtolower($controller);

// Set controller class name
$controller = ucfirst($controller).'_Controller';

if(!class_exists($controller, FALSE))
{
// If the file doesn't exist, just return
if (($filepath = Kohana::find_file('controllers', $controller_file)) === FALSE)
return FALSE;

// Include the Controller file
require_once $filepath;
}

// Run system.pre_controller
Event::run('dispatch.pre_controller');

// Initialize the controller
$controller = new $controller;

// Run system.post_controller_constructor
Event::run('dispatch.post_controller_constructor');

return new Dispatch($controller);
}
public function __construct(Controller $controller)
{
$this->controller=$controller;

}

public function __get($key)
{
if($key=='controller')
{
return $this->$key;
}
else
{
return $this->controller->$key;
}
}

public function __set($key,$value)
{
$this->controller->$key=$value;
}

public function __toString()
{
return $this->render();
}

public function render()
{
return (string) $this->controller;
}

public function __call($name,$arguments=null)
{
if(method_exists($this->controller,$name))
{
return $this->method($name,$arguments);
}
return false;
}

public function method($method,$arguments=null)
{
if(!method_exists($this->controller,$method))
return false;

if (method_exists($this->controller,'_remap'))
{
// Make the arguments routed
$arguments = array($method, $arguments);

// The method becomes part of the arguments
array_unshift($arguments, $method);

// Set the method to _remap
$method = '_remap';
}

ob_start();

if(is_string($arguments))
{
$arguments=array($arguments);
}

switch(count($arguments))
{
case 1:
$result=$this->controller->$method($arguments[0]);
break;
case 2:
$result=$this->controller->$method($arguments[0], $arguments[1]);
break;
case 3:
$result=$this->controller->$method($arguments[0], $arguments[1], $arguments[2]);
break;
case 4:
$result=$this->controller->$method($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
break;
default:
// Resort to using call_user_func_array for many segments
$result=call_user_func_array(array($this->controller, $method), $arguments);
break;
}

// Run system.post_controller
Event::run('dispatch.post_controller');

if($result!=NULL)
{
$result=ob_get_contents();

ob_end_clean();
}

return $result;
}

}

now the above you can save under your library directory in your application for ease of use as something like dispatch.php.

so to solve the problem of the login form – in my default home template i’ve added <?php echo $this->template->partial = new View('log/generateform');?>

in the generateform view i called i will use the following:

<?php
//call the log controller
$dispatch = Dispatch::Controller('log');
//call the generateForm method
$dispatch->method('generateForm');
//return the result from the template to the view
echo $dispatch->template;
?>

the above will call the  log controller and the method generateForm, in the controller, you can specify the template, for e.g. <?php public $template = 'kohana/partial/navigation';?> and then in the action / controller assign any values to be used by the template view/presentation.

oh, and one more cool thing with the above is that you can also call the method using parameters for e.g. <?php $dispatch->generateForm(array('title'='login'));?>

recap

as i said in the beginning, if anyone feels that i’ve overstepped the mvc divide, please let me know, i’m sure that with the assigning of a controller/action from within a view is totally acceptable, since only your presentation logic is affected by it really, and the controller still decides on what is available for the view to pull, without having adjust your controllers to cater for everything.

and if someone knows of a quicker method in kohana, also do let me know.