[Solar-talk] Handling 404 and exceptions

Paul M Jones pmjones at paul-m-jones.com
Sat Mar 1 09:22:21 CST 2008


On Mar 1, 2008, at 07:53 , Leo Chiao wrote:

> Sorry for such a basic question, but I wanted to know what is the  
> recommended way of handling 404 errors and exceptions in Solar.   
> From what I can tell, specifying the default controller through the  
> config makes that controller be displayed in 404 scenarios where a  
> controller isn't found.

That is generally the recommended way; i.e., when a page is not found,  
fall back to the default controller and action.  There are two places  
you can override that behavior, though:

* Solar_Controller_Front::_notFound() executes when it can't find the  
requested page controller.

* Solar_Controller_Page::_forwardActionMethod() executes when it can't  
find a method for the requested action.

(Looking at it now, the name _forwardActionMethod() describes exactly  
what is happening, but is not very intuitive -- maybe i should rename  
that to something more inviting.  Maybe use Front::_pageNotFound and  
Page::_actionNotFound() as the names?)



> This config setup appears to work for what I want to do, but I'm not  
> sure if it's the best or proper way:
>
> $config['Solar_Controller_Front']['default'] = 'error';
> $config['Solar_Controller_Front']['routing'] = array(
>         '' => 'Vendor_App_News',
> 	'error'	=> 'Vendor_App_Error'
> );
>
>
> Using this config, accessing the project by it's base url would load  
> the news controller, whereas the error controller would load for non- 
> existent controllers.  Is this the right way to accomplish what I am  
> trying to do?

Huh -- I've not seen a setup like that before.  Kinda nice.  :-)



> On a related note to handling 404s, what is the recommended way to  
> capture all exceptions and display a generic error message page?

This is an easy one:  You would override the  
Solar_Controller_Page::_exceptionDuringFetch() method in your base  
page controller (e.g., Vendor_App).  The Solar_App method for that  
looks like this:

     /**
      *
      * Shows the "exception during fetch" page.
      *
      * @param Exception $e The exception encountered during fetch().
      *
      * @return Solar_Response A response object with a 500 status  
code and
      * a page describing the exception.
      *
      */
     protected function _exceptionDuringFetch(Exception $e)
     {
         $this->errors[] = $e;
         $this->_layout = null;
         $this->_view = 'exception';
         $this->_format = null;
         $this->_response->setStatusCode(500);

         $this->_render();
         return $this->_response;
     }

There is a related "Solar/App/Base/Views/exception.php" file for  
showing a nice error page.


-- pmj


More information about the Solar-talk mailing list