PHP Developers Network
http://forums.devnetwork.net/

Catching an exception in MVC type structure.
http://forums.devnetwork.net/viewtopic.php?f=19&t=141709
Page 1 of 1

Author:  social_experiment [ Tue Sep 01, 2015 7:29 am ]
Post subject:  Catching an exception in MVC type structure.

I have the following pages:

1. Class page
2. 'Controller' page
3. index page

On the controller page, I have the following code

Syntax: [ Download ] [ Hide ]
<?php
  // included class page, created object
try {          
   $data = $prodObj->getRandomProducts();
   if ($data !== FALSE) {
     $productArray = $data;
   }
   else {
      // no data in array, 0 records.
   }           
}
   catch (Exception $e) {
     var_dump($e->getTrace());
   }
?>
 


On the index page I want to use $productArray to display products.

Syntax: [ Download ] [ Hide ]
  // index page, display data
  <p><?php echo $productArray['product_id']; ?></p>
 


Where do I catch the exception, on the controller page or on the index page? Could I catch the exception inside the class, in the wrapper function?

Author:  requinix [ Tue Sep 01, 2015 3:02 pm ]
Post subject:  Re: Catching an exception in MVC type structure.

Depends how you want to react to the exception.

If you want to show an error page, that suggests changing the view and it should be done in the controller.
If you want to show the same page but with an error message, that suggests doing it in the view. However I think I'd use a second view for it instead of passing the information along to your index view.

Not sure what the "wrapper function" is.

Author:  social_experiment [ Tue Sep 01, 2015 10:56 pm ]
Post subject:  Re: Catching an exception in MVC type structure.

It's the public function inside the class that calls a protected function

Author:  jkon [ Sun Sep 06, 2015 4:29 am ]
Post subject:  Re: Catching an exception in MVC type structure.

From the 3 aspects that you described ( 1. Class page 2. 'Controller' page 3. index page) I can't understand what is the “top” . Generally , exceptions should be cached through the top level component of an application. By top I mean the one that is in the core of the app and generates all the others (e.g. the index.php isn't the top normally because it is just the instantiator of the front controller in MVC). That doesn't mean that each tier of the app should try – catch , the opposite. They should if they have to do something with that exception and then through it back to parent. If children's or other tiers only catch the exception to through it up (or again) there is no meaning to do so and is a sign of a week understanding of what Exceptions are (in my opinion).

Author:  Christopher [ Sun Sep 06, 2015 11:49 am ]
Post subject:  Re: Catching an exception in MVC type structure.

It seems like $prodObj->getRandomProducts() should generate the exception and then whatever code above it can deal with the exception. Having that if() indicates that maybe you are calling things in the wrong place.

Author:  social_experiment [ Sun Sep 06, 2015 12:16 pm ]
Post subject:  Re: Catching an exception in MVC type structure.


Author:  Christopher [ Sun Sep 06, 2015 3:48 pm ]
Post subject:  Re: Catching an exception in MVC type structure.

The question I would ask is: what is the exception communicating? Is it that some actual error occurred? Or is it just that no product records were found? For me the difference is what the View code displays to the user. If it is an error then I want to display something that the user can tell tech support that communicates the problem. If it is just no records then just display that no records were found.

Author:  social_experiment [ Mon Sep 07, 2015 3:10 am ]
Post subject:  Re: Catching an exception in MVC type structure.


Author:  Christopher [ Mon Sep 07, 2015 5:33 pm ]
Post subject:  Re: Catching an exception in MVC type structure.

It seem like the index page that uses the data should get the $prodObj object. It can then catch the exception if there is a database error, or separately check if no results are returned and let the user know about that.

Author:  social_experiment [ Tue Sep 08, 2015 12:16 am ]
Post subject:  Re: Catching an exception in MVC type structure.


Author:  Christopher [ Tue Sep 08, 2015 10:00 pm ]
Post subject:  Re: Catching an exception in MVC type structure.

Yes, I guess. It is still not clear which file is which.

Author:  social_experiment [ Wed Sep 09, 2015 12:05 am ]
Post subject:  Re: Catching an exception in MVC type structure.

this would be on the index page

Author:  Christopher [ Wed Sep 09, 2015 9:47 am ]
Post subject:  Re: Catching an exception in MVC type structure.

I guess my question is: Why do you need an exception? It seems like if($data) is all you need. Either some random products are found, or the array is empty. If there is a syntax error it will show up in the logs. An SQL error should be fixed in development. So either display the random products or show that there are none.

Author:  social_experiment [ Wed Sep 09, 2015 3:13 pm ]
Post subject:  Re: Catching an exception in MVC type structure.


Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/