Welcome Guest, Not a member yet? Register   Sign In
$this->load->view and exceptions
#1

[eluser]Unknown[/eluser]
Hi,

I had hoped:
$this->load->view('missing_view')
would throw an exception, or return an error code. It seems to do neither. Instead, it internally generates an error page, and does an exit (see: Loader->_ci_load()). I'm not used to API calls that behave this way. Was there a good reason for this design decision? I know some people dislike exceptions, but throwing exceptions when resources are missing is pretty common (for example, most file system implementations use exceptions for "file not found"). Also, the idea that an API call terminates my program is... worrysome...

Why did I hope this? I have pages that can be customized on a per customer basis, yet I have a default look & feel. So, I was going to do something like
try {
$this->load->view('pagename_'.$customer_name);
} catch(Exception $e) {
$this->load->view('pagename_default');
}

or
if(!$this->load->view('pagename_'.$customer_name)){
$this->load->view('pagename_default');
}


Is there a more clever solution to my use case? Using data from the controller to parameterize the view feels very wrong to me, as it's only the view that is changing. Plus, the data to track if a customized view exists or not will just add another piece of needless data that can be overlooked.

I could change Loader->_ci_load() to throw an exception (and I did for grins, and it worked), but I'm loathe to change system libraries.

Thanks,

will
#2

[eluser]Unknown[/eluser]
To answer my own question, I check to see if the file exists manually using the APPPATH constant. E.g.:
file_exists(APPPATH . 'views/'.$view.'.php')




Theme © iAndrew 2016 - Forum software by © MyBB