在Zend Framework应用程序中从模型/视图/控制器抛出异常 [英] Throwing exceptions from model/view/controller in a Zend Framework application
问题描述
在Zend Framework库中,当前的实践(大约1.10.8)是库组件抛出扩展Zend_Exception
的异常.
In the Zend Framework library, the current practice (circa 1.10.8) is that library components throw exceptions that extend Zend_Exception
.
例如Zend_Layout
组件抛出Zend_Layout_Exception
在我自己的ZF库中,在其中添加我自己的ZF组件或扩展现有组件的地方,我抛出了Mylibrary_Exception
(当然,这并不是真正的称呼:)
In my own ZF library, where I'm adding my own ZF components or extending existing components, I'm throwing a Mylibrary_Exception
(it's not really called that of course :)
我看到他们将在ZF 2.0中进行一些更改
I can see that they're going to change some of that in ZF 2.0
http://framework.zend.com /wiki/display/ZFDEV2/Proposal + for + Exception + in + ZF2
我的实际问题是这样的:
My actual question is this:
在我的控制器/模型/视图中遍历我的MVC应用程序,如果我需要引发异常(这种情况很少见,因为显然我将以其他方式处理预期的错误)-但如果我确实需要在这里抛出异常,ZF的最佳实践是什么?
Throughout my MVC application in my controllers/models/views, if I need to throw an exception (and this will be rare, because obviously I'll be handling expected errors in a different way) - BUT if I do need to throw an exception here, what is the best practice in ZF?
我应该
throw new Exception("this is an exception");
或者应该在我的ZF模块中创建Exception类,类似于ZF库的组织方式.即它们对每个库组件都有异常类,我是否应该对每个应用程序模块都有异常类?
or should I be creating Exception classes in my ZF modules, similar to how the ZF library is organised. i.e. they have exception classes for each library component, should I have exception classes for each application module?
application/modules/user/controllers/UserController.php
application/modules/user/controllers/UserController.php
application/modules/user/forms/UserForm.php
application/modules/user/forms/UserForm.php
application/modules/user/models/User.php
application/modules/user/models/User.php
application/modules/user/views/scripts/index.phtml
application/modules/user/views/scripts/index.phtml
application/modules/user/exceptions/Exception.php(User_Exception类)
application/modules/user/exceptions/Exception.php (class User_Exception)
application/modules/user/exceptions/SuperexampleException.php(类User_Exception_Superexample)
application/modules/user/exceptions/SuperexampleException.php (class User_Exception_Superexample)
我以前从未在ZF中看到任何人做过类似的事情,所以我不确定这是否是个好主意.
I've never seen anyone do anything like that before in ZF, so I'm not sure if it's a good idea or not.
更新:
为了进一步澄清我的问题-在应用程序的MVC部分(而不是库)中引发异常时-是否有关于使用特定异常类(如库那样)而不是仅使用通用Exception类的约定?
To clarify my question further - when raising exceptions in the MVC part of the application (as opposed to the library) - are there any conventions regarding using specific exception classes (like the library does) vs just using the generic Exception class?
推荐答案
我建议您使用一些通用"异常,例如(InvalidParameter,InvalidRange).好的起点是
I'd suggest having some "generic" exceptions like (InvalidParameter, InvalidRange) Good starting point is czech framework called Nette.
然后添加一些特定于应用程序/用户/操作的例外-
Then add some app/user/action specific exceptions - like
- InvalidUserInputException-适用于用户输入无效值(例如,在eshop中为"-1"作为数量的状态)的状态
- NotFoundException-用于未找到某些内容的州
- 等
为这些异常添加特殊的成员变量-如 id 或 data -并使用它们来将异常用作错误消息的传递.
Add special member variables to those exceptions - like id or data - and use them to employ exceptions as delivery for error messages.
$e = new NotFoundException('There is no user %s in here!');
$e->setUser('Frank');
throw $e;
// later in error controller
if ($e instanceof NotFoundException) {
// set header to 404, display error message, etc
}
好的是,您可以随时扩展异常,并可以随时将其捕获到错误控制器中(甚至更好的是使用接口)
Good thing is that you can extend the exceptions anyhow you want and catch them in error controller anyhow you want (even better it's using Interfaces)
class AccessDeniedExceptionimplements ILogableException
//...
throw new AccessDeniedException();
//in EC
if ($e instanceof ILoggableException) {
$this->getLogger()->log($e->getLogMessage());
}
这篇关于在Zend Framework应用程序中从模型/视图/控制器抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!