[Solar-talk] Change to Solar_Controller_Page::fetch()
Paul M Jones
pmjones at ciaweb.net
Fri Mar 16 15:23:15 PDT 2007
Hi Travis and Rodrigo,
> I'm thinking it'd be useful for _postRun() to run even if _forward()
> throws an exception. That gives you the ability to recover to
> something
> more useful than an exception at the app level. This is especially
> important if you treat _preRun()/_postRun() like setup()/teardown() in
> unit tests where they do something that alters the environment or just
> tracks it.
Per your related suggestions, I've added something like that to
branches/app Solar_Controller_Page. The code looks like this:
public function fetch($spec = null)
{
try {
// load action, info, and query properties
$this->_load($spec);
// prerun hook
$this->_preRun();
// action chain, with pre- and post-action hooks
$this->_forward($this->_action, $this->_info);
// postrun hook
$this->_postRun();
// render the view and layout, with pre- and post-render
hooks
$this->_render();
// set the Content-Type based on the format
$this->_setContentType();
// done, return the response headers, cookies, and body
return $this->_response;
} catch (Exception $e){
// an exception was thrown somewhere, attempt to rescue it
return $this->_exceptionDuringFetch($e);
}
}
The _exceptionDuringFetch() method by default just re-throws the
exception, but the new Solar_App_Base does a little more with it:
protected function _exceptionDuringFetch(Exception $e)
{
$this->errors[] = $e;
$this->_layout = null;
$this->_view = 'exception';
$this->_format = null;
$this->_response->setStatus(500);
$this->_render();
return $this->_response;
}
So it presents a nicely-formatted "Server Error" page with the right
response status code and everything.
--
Paul M. Jones <http://paul-m-jones.com>
Solar: Simple Object Library and Application Repository
for PHP5. <http://solarphp.com>
Join the Solar community wiki! <http://solarphp.org>
Savant: The simple, elegant, and powerful solution for
templates in PHP. <http://phpsavant.com>
More information about the solar-talk
mailing list