2013-05-28 2 views
0

정의에서 XML 오류를 렌더링 : 나는 XML 형식으로 렌더링하는 사용자 지정 오류 렌더러를 만드는거야 Using a custom renderer with Exception.renderer to handle application exceptionsCakePHP는 여기에 정보를 바탕으로 Exception.renderer

.

public function render() { 
    if (isset($this->controller->request->params['xml'])) { 
     $this->controller->viewClass = "MyXml"; 
     $error = array(
      'app' => array(
       'error' => 'An unexpected error has occured.' 
      ) 
     ); 
     $this->controller->set('error', $error); 
     $this->controller->set('_serialize', 'error'); 
    } 
} 

그러나 아무것도 반환됩니다 :

다음은 응용 프로그램/lib 디렉토리/오류/AppExceptionRenderer의 렌더링 함수에 대한 샘플 코드입니다. if 조건 내에서 일부 에코를 수행했음을 보여줍니다.

ViewClass가 AppExceptionRenderer :: render() 단계에서 초기화되지 않았기 때문에 그렇습니까?

오류가 없었습니다.
"MyXml"viewClass도 일반 컨트롤러에서도 완벽하게 작동합니다.

답변

1

그래서 렌더링을 놓치고 메서드를 보냈습니다.
다음은 전체 작동 예제입니다.

<?php 
class AppExceptionRenderer extends ExceptionRenderer { 

    public function __construct($exception) { 
      parent::__construct($exception); 
    } 

    public function render() { 

     // Handle errors 
     if (isset($this->controller->request->params['xml'])) { 
      Cakelog::error($this->error->getMessage()); 
      $this->controller->viewClass = "MyXml"; 
      $error = array(
       'app' => array(
        'error' => 'An illegal operation has been detected.' 
       ) 
      ); 
      $this->controller->set('error', $error); 
      $this->controller->set('_serialize', 'error'); 
      $cakeResponseObject = $this->controller->render(); 
      $this->controller->response->send($cakeResponseObject); 
     } else { 
      if ($this->method) { 
       call_user_func_array(array($this, $this->method), array($this->error)); 
      } 
     } 
    } 
}