tags:

views:

144

answers:

1

In methods of controllers, there are usually many checks for "doing the right thing".
For instance, I check if user is accessing the right data.

However, I'm wondering what is the best way to return or exit from these methods when errors do occur. I've been using just simple return statement, but is there anything that's more CakePHP-like and follow the framework's design? or is simple return/exit statement good enough?

+1  A: 

This really depends on what you want to do.

If you are processing a form post, and the data validation failed, a simple return is enough.

If the validation has passed, a redirect is more appropriate (to prevent double posting the data with potentially terrible consequences).

In case of a really fatal error, potentially not even caused by your own code (unlikely, right?:)), cake gives you a few options of handling those errors, via AppController::appError() or the whole class called AppError.

See more info here:

http://book.cakephp.org/view/154/Error-Handling

dr Hannibal Lecter
When you say a simple return is enough, do you mean actually returning a value (true/false, etc.), or just letting the code block exit naturally?
Travis Leleu
In a standard cake request, return value is not needed nor used. By returning from the controller action, you're basically telling cake "I'm ready for you to render my page". There are exceptions to this (like switching auto rendering off), but that's another question :)
dr Hannibal Lecter