dongya2029 2012-06-22 08:22
浏览 6
已采纳

传递zend错误控制器额外参数

I am attempting to provide user friendly error messages via my web application for certain errors. I want to be able to pass the throw exception call and extra paramter, such like "USER" so the error controller gives this paramter to the view and displays a different style page with maybe a more friendly color and a meaningful message, but I can't figure out how to pass extra arguments. Here is the current setup:

Some check in one of the controllers:

throw new Zend_Controller_Exception("User not found",403);

Error controller (ErrorController.php):

class ErrorController
    extends Zend_Controller_Action
    {
        public function errorAction()
        {
        $errors = $this->_getParam('error_handler');
        switch ($errors->type) {

etc.

So I want to be able to do like:

$this->view->type = $this->_getParam('type');

in the controller, so I can do this in the view:

if($this->type == "USER") {
    some css stuff
    echo $this->exception;
  • 写回答

1条回答 默认 最新

  • douyangqian5243 2012-06-22 08:54
    关注

    Sounds like you could:

    1. Create a custom exception class (My_Exception_UserFriendly or the like) that accepts your other parameters

    2. In a controller (or elsewhere) throw this exception with your custom params, and

    3. Use an instanceof check in your ErrorController to populate your view variables.

    And, as you already note, make sure your view checks for the presence of that user-friendly data before attempting to render it.

    Update

    Actually, reading more closely, it doesn't look like you need the exception to carry any additional information. Simple creating a custom class should be sufficient for your detection/rendering purposes.

    You can create an empty (!) exception class in library/My/Exception/UserFiendly.php:

    class My_Exception_UserFriendly extends Exception
    {
    }
    

    Then when you encounter an error that you would like to show to the user in a friendly way, just throw an exception of this type:

    if ($rainyDay){
        throw new My_Exception_UserFriendly('Seeing some inclement weather, I\'m afraid');
    }
    

    Then in your ErrorController::errorAction():

    $exception = $this->_getParam('error_handler')->exception;
    if ($exception instanceof My_Exception_UserFriendly){
        $this->view->friendlyErrorMessage = $exception->getMessage();
    }
    

    Finally, down in your view-script error/error.phtml:

    <?php if ($this->friendlyErrorMessage): ?>
    <h2>With Humble Apologies, Valued User</h2>
    <p><?= $this->escape($this->friendlyErrorMessage) ?></p>
    <?php endif; ?>
    

    In fact, some would argue that the specific message we render is purely a view consideration. In that case, you could create a custom exception - perhaps extending this generic UserFriendly exception - for each friendly message you wish to support. Then in your ErrorController, detect the specific sub-type and set a key in the view identifying that subtype. Then, in the view, render the specific friendly message corresponding to the given key. Might be overkill just for the sake of purity, but throwing it out there for those who value those considerations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应