class Controller_404 extends Controller_Template { /** * The basic welcome message * * @access public * @return Response */ public function action_index() { $this->template->title = ViewModel::get_message(); $this->template->content = Response::forge(View::forge('404'), 404); //return Response::forge(ViewModel::forge('404'), 404); } }
class View_404 extends ViewModel { /** * Prepare the view data, keeping this in here helps clean up * the controller. * * @return void */ public function view() { $messages = array('Aw, crap!', 'Bloody Hell!', 'Uh Oh!', 'Nope, not here.', 'Huh?'); $this->get_message = function() { return $messages[array_rand($messages)]; }; } }
$this->template->content = Response::forge(ViewModel::forge('404'), 404);
Ashley Bye wrote on Tuesday 28th of February 2012:How, therefore, do I use a ViewModel as in the Welcome example where the ViewModel is used to decide upon the message?
class View_Test extends ViewModel { public function view() {} static public function get_message() { $messages = array('Aw, crap!', 'Bloody Hell!', 'Uh Oh!', 'Nope, not here.', 'Huh?'); return $messages[array_rand($messages)]; } public function before($data = null) { parent::before(); // Without this line, templating won't work! $this->set('title_before', $this->get_message()); } }Notice also the required view method. You'll also have to create a standard view file since it's also required. It can be empty. It just has to exist. In the controller, if you want to access a viewModel property, you have to either intantiate it first using forge or call it from child class. The following code shows examples of both approaches.
class Controller_Test extends Controller_Template { public function action_index() { $viewModel = ViewModel::forge('test'); $this->template->title = View_Test::get_message(); $this->template->title_before = $viewModel->get('title_before'); $this->template->content = Response::forge(View::forge('404'), 404); $this->response->body = $this->template; } }As far as your random message goes, save yourself some headaches and just create a method.
It looks like you're new here. If you want to get involved, click one of these buttons!