I've been digging into the K03 beta with glee and have discovered that requests are chain-able.
I'm not sure if this is exactly how Shadowhand intends things to be done but the following example works.
// Here is a test controller
class Controller_Test extends Controller
{
// Here is my default controller action
public function action_index()
{
// Create a base view
$view = View::factory('theme');
// Assign another view to the variable menu in the base view
$view->menu = View::factory('menu');
// Create a new Kohana request and execute !
// Here I am executing the default action from Controller_Welcome
$welcome = Request::factory('welcome')->execute();
// Assign the response from Controller_Welcome to the **content** variable
// in my base view
$view->content = $welcome->response;
// Assign another variable just with a string
$view->footer = 'this is my footer';
// Render the view.
echo $view->render();
}
}
Hopefully Shadowhand will correct me if I'm not using the Request factory as intended.
That is almost exactly how it is done, except that your last line should read:
$this->request->response = $view->render();
Here's another working example. KO3 Template/Theme/Website Controller
class Controller_Website extends Controller
{
public $template = NULL; // you could set a default view
public $auto_render = TRUE;
public function before()
{
if( ! Request::$is_ajax && $this->template ){
$this->template = View::factory($this->template);
}
}
public function after()
{
if( ! Request::$is_ajax && $this->auto_render && $this->template ) {
$this->request->response = $this->template;
}
}
}
Controller extending the Website Controller
class Controller_Welcome extends Controller_Website {
public $template = 'theme'; // template view
public function action_index()
{
$this->template->menu = Request::factory('menu')->execute()->response;
$this->template->content = View::factory('welcome');
$this->template->footer = Request::factory('footer')->execute()->response;
}
} // End Welcome
again. only pulled together by reading the code comments so there could be a better way to do it
There is already a Controller_Template that ships with KO3.
class Controller_Welcome extends Controller {
public function action_test()
{
$this->request->response = Request::factory('welcome/xxx')->execute()->response;
}
public function action_xxx()
{
$this->request->response = View::factory('v_welcome')->render();
}
} // End Welcome
The HMVC functionality looks very promising.
If all of your controllers extend the new Controller_Template, do you need to specifically call parent::before() or parent::after() if you plan to override those methods in any of your child controllers?
Also, what is the suggested way to simulate the 2.x $auto_render = FALSE functionality? Should we override the after() method and set $this->request->response = NULL ?
You should always call parent:: methods if they exist.
Overload the after() method if you don't automatically want to add the response. I should probably add the $auto_render to the template controller though.
return $contacts;
class Controller_Home extends Controller_Default {
public function action_index() {
$contacts = Request::factory('contacts')->execute()->response;
$this->template->contacts = View::factory('contacts', array('content' => $contacts))->render();
// OR
$this->template->contacts = View::factory('contacts')->set('content', $contacts)->render();
}
}
View Object ( [_file:protected] => /home/myuser/bin/kohana/newApp/views/templates/default.php [_data:protected] => Array (
[title] => Blah Blah [meta_keywords] => my, keywords, separated [meta_description] => my description
[meta_copywrite] => 12/10/2010 [header] => [content] => [footer] =>
[styles] => Array (
[http://www.somewhere.org/css/screen.css] => screen [http://www.somewhere.org/css/print.css] => print
[http://www.somewhere.org/css/ie.css] => ie [http://www.somewhere.org/css/style.css] => style )
[scripts] => Array ( [0] => http://www.somewhere.org/js/jquery-1.4.4.min ) ) )
class Controller_Home extends Controller_Default {
public function action_index() {
$set_views = array();
$contacts = Request::factory('contacts')->execute()->response;
$set_views['contacts'] = View::factory('contacts')->set('contacts', $contacts)->render();
$this->template->content = View::factory('index', $set_views);
}
}
class Controller_Default extends Controller_Template {
public $template = 'templates/default';
public function before() {
parent::before();
if($this->auto_render) {
$this->template->title = 'Blah Blah';
$this->template->meta_keywords = 'my, keywords, separated';
$this->template->meta_description = 'my description';
$this->template->meta_copywrite = '12/10/2010';
$this->template->header = '';
$this->template->content = '';
$this->template->footer = '';
$this->template->styles = array();
$this->template->scripts = array();
}
}
public function after() {
if($this->auto_render) {
$styles = array(
url::base() . 'css/style.css' => 'style',
url::base() . 'css/ie.css' => 'ie',
url::base() . 'css/print.css' => 'print',
url::base() . 'css/screen.css' => 'screen',
);
$scripts = array(url::base() . 'js/jquery-1.4.4.min');
$this->template->styles = array_reverse(array_merge($this->template->styles, $styles));
$this->template->scripts = array_reverse(array_merge($this->template->scripts, $scripts));
}
parent::after();
}
}
class Controller_Contacts extends Controller_Default {
public function action_index() {
$get_contacts = new Model_Contacts();
$contacts = $get_contacts->get_contacts();
return $contacts;
}
}
class Model_Contacts extends Model {
public function __construct() {
parent::__construct();
}
public function get_contacts() {
$query = DB::select()->from('contact');
return $query->execute('port');
}
}
<?php
print_r($contacts);
?>
<?php
echo $contacts;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Language" content="en-us" />
<title><?php echo $title;?></title>
<meta name="keywords" content="<?php echo $meta_keywords;?>" />
<meta name="description" content="<?php echo $meta_description;?>" />
<meta name="copyright" content="<?php echo $meta_copywrite;?>" />
<?php foreach($styles as $file => $type) { echo HTML::style($file, array('media' => $type)), "\n"; }?>
<?php foreach($scripts as $file) { echo HTML::script($file, NULL, TRUE), "\n"; }?>
</head>
<body>
<div id="container">
<?php echo $header;?>
<?php echo $content;?>
<?php echo $footer;?>
</div>
</body>
</html>
class Controller_Contacts extends Controller_Default {
public function action_index() {
$get_contacts = new Model_Contacts();
$contacts = $get_contacts->get_contacts();
$this->request->response = $contacts;
}
}
class Controller_Home {
public function action_index() {
$contacts = Model_Contacts::get_contacts();
$this->template->content = View::factory('contacts')
->set('contacts',$contacts);
}
}
It looks like you're new here. If you want to get involved, click one of these buttons!