2014-02-13 6 views
0

우리는 Cake 구성 요소에 정적 메서드가 있습니다. 이 구성 요소가 특정 오류를 발생시키는 경우 작업은 사용자를 로그인 페이지로 리디렉션하는 것이 었습니다. 현재 작동하는 솔루션은 다음과 같습니다.케이크 : 구성 요소에서 정적 메서드로 컨트롤러에 액세스

class SomeComponent extends Component { 

    static $controllerObject; 

    function startup(&$controller) {   
     SomeComponent::$controllerObject =& $controller; 
    } 

(...) 

    public static function decodeResponse($response) { 
     if($response == null || trim($response) == '') { 
      return null; 
     } 
     $result = json_decode($response, true); 
     if($result == null) { 
      throw new FatalErrorException('Could not parse api server response: ' . $response); 
     } 
     if(isset($result['error'])) { 
      if ($result['error'] === "IncorrectCredentialsException") { 
       self::$controllerObject->Session->destroy();     
       self::$controllerObject->Session->setFlash(__('Your session has ended. Please log in again.', true), 'default', array(), 'error');         
       self::$controllerObject->redirect(array(
        'language' => Configure::read('Config.language'), 
        'controller' => 'users', 
        'action' => 'login' 
       )); 

      } 
      else { throw new ApiServerException($result); } 
     } 
     return $result; 
    } 

그러나 소프트웨어 품질을 담당하는 팀 동료는 만족스러운 해결책을 찾지 못합니다. 그는 "pls는 컨트롤러를 디코드 메서드로 전달하는 더 좋은 방법을 찾았습니다. 컨트롤러를 정적 변수로 설정하는 것이 최선의 방법은 아닙니다."

더 좋은 방법이 있습니까?

답변

1

나는 문제가 당신의 방법이 두 가지 다른 일을한다고 생각한다 : 디코딩과 오류 처리. 메서드 내에서 IncorrectCredentialsException을 처리하는 대신 다른 예외를 처리하는 곳으로이 기능을 옮기고 메서드에 IncorrectCredentialsException을 던지십시오. 이 변경으로 더 이상 정적 메소드에서 컨트롤러에 액세스 할 필요가 없습니다.

관련 문제