在CakePHP中,从控制器方法退出或返回的好方法是什么?

时间:2022-10-21 22:16:38

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?

但是,我想知道在发生错误时返回或退出这些方法的最佳方法是什么。我一直在使用简单的return语句,但有没有更像CakePHP的东西并遵循框架的设计?或简单的退货/退货声明是否足够好?

1 个解决方案

#1


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.

如果真的有致命的错误,甚至可能不是由你自己的代码引起的(不太可能,对吧?:)),cake会给你一些处理这些错误的选项,通过AppController :: appError()或整个类AppError。

See more info here:

在这里查看更多信息:

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

#1


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.

如果真的有致命的错误,甚至可能不是由你自己的代码引起的(不太可能,对吧?:)),cake会给你一些处理这些错误的选项,通过AppController :: appError()或整个类AppError。

See more info here:

在这里查看更多信息:

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