Yii: Catching all exceptions for a specific controller

前端 未结 5 1756
日久生厌
日久生厌 2020-12-25 15:08

I am working on a project which includes a REST API component. I have a controller dedicated to handling all of the REST API calls.

Is there any way to catch all exc

5条回答
  •  隐瞒了意图╮
    2020-12-25 15:21

    You can completely bypass Yii's default error displaying mechanism by registering onError and onException event listeners.

    Example:

    class ApiController extends CController
    {
      public function init()
      {
        parent::init();
    
        Yii::app()->attachEventHandler('onError',array($this,'handleError'));
        Yii::app()->attachEventHandler('onException',array($this,'handleError'));
      }
    
      public function handleError(CEvent $event)
      {        
        if ($event instanceof CExceptionEvent)
        {
          // handle exception
          // ...
        }
        elseif($event instanceof CErrorEvent)
        {
          // handle error
          // ...
        }
    
        $event->handled = TRUE;
      }
    
      // ...
    }
    

提交回复
热议问题