2014-09-02 2 views
1

Phalcon은 콘텐츠 협상을 즉시 지원합니까? 아니면 구현하기 쉬운 솔루션이 있습니까? 나는 그물을 샅샅이 뒤지고 그것을 보지 않고있다.PhalconPHP : 콘텐츠 협상?

감사합니다.

답변

1

짧은 대답은 '노 및 그 하나님 께 감사를드립니다 없다, 또는 우리는 비 주요 구성 요소 :

당신은 쉽게 DI로, Negotiation 같은 기존 라이브러리를 연결하여 사용할 수 있습니다 또 다른 100 개 버그가 줄 나중에 앱 전체에 걸쳐

$di->setShared('negotiator', function(){ 
    return new \Negotiation\Negotiator(); 
}); 

$bestHeader = $di->getShared('negotiator')->getBest('en; q=0.1, fr; q=0.4, fu; q=0.9, de; q=0.2'); 

Phalcon에 의해 차단하지 않고, 예에서 기본 서버 설정 (htaccess로/Nginx와)과 정적 파일이 그대로 제공 될 수 있음을 유념하시기 바랍니다. 따라서 서버의 서버 파일에 대해서는 모든 요청을 앱을 거치지 않고 별도의 컨트롤러/액션을 만들어 처리하는 것이 가장 좋습니다.

편집 :는 일반적인 구분 (헤더, PARAM, 방법)에 따라 XML 또는 JSON 중 하나를 전송하여 응용 프로그램을 활성화하는 방법에 대한 간단 있다면

, 당신은 쉽게 외부 프레임 워크없이 수행 할 수 있습니다. 많은 전략이 있습니다. 가장 간단한 방법은 Dispatcher::dispatch()을 가로 채고 어떤 컨텐츠를 반환할지 결정하고 이에 따라보기 및 응답을 구성합니다. 나머지는 Phalcon이 수행합니다.

/** 
* All controllers must extend the base class and actions must set result to `$this->responseContent` property, 
* that value will be later converted to the appropriate form. 
*/ 
abstract class AbstractController extends \Phalcon\Mvc\Controller 
{ 

    /** 
    * Response content in a common format that can be converted to either json or xml. 
    * 
    * @var array 
    */ 
    public $responseContent; 
} 

/** 
* New dispatcher checks if the last dispatched controller has `$responseContent` property it will convert it 
* to the right format, disable the view and direcly return the result. 
*/ 
class Dispatcher extends \Phalcon\Mvc\Dispatcher 
{ 

    /** 
    * @inheritdoc 
    */ 
    public function dispatch() 
    { 
     $result   = parent::dispatch(); 
     $headerAccept  = $this->request->getHeader('Accept'); 
     $headerContentType = $this->request->getHeader('Content-Type'); 
     $lastController = $this->getLastController(); 

     // If controller is an "alien" or the response content is not provided, just return the original result. 

     if (!$lastController instanceof AbstractController || !isset($lastController->responseContent)) { 
      return $result; 
     } 

     // Decide what content format has been requested and prepare the response. 

     if ($headerAccept === 'application/json' && $headerContentType === 'application/json') { 
      $response = json_encode($lastController->responseContent); 
      $contentType = 'application/json'; 
     } else { 
      $response = your_xml_convertion_method_call($lastController->responseContent); 
      $contentType = 'application/xml'; 
     } 

     // Disable the view – we are not rendering anything (unless you've already disabled it globally). 

     $view->disable(); 

     // Prepare the actual response object. 

     $response = $lastController->response 
      ->setContent($response) 
      ->setContentType($contentType); 

     // The returned value must also be set explicitly. 

     $this->setReturnedValue($response); 

     return $result; 
    } 
} 

// In your configuration you must insert the right dispatcher into DI. 

$di->setShared('dispatcher', function(){ 
    return new \The\Above\Dispatcher(); 
}); 

그냥 dispatch loop events을 사용하여 동일하게 달성 할 수 있다고 생각했습니다. 이론적 인 해결책은 좀 더 우아 해 보일 수도 있지만 결코 시도하지 않았으므로 직접 시도해 볼 수도 있습니다.

+0

Gotcha. 콘텐츠 협상, 특히 포맷 협상은 API/마이크로 애플리케이션에서 중요하다고 생각합니다. 포인터 주셔서 감사합니다. –

+0

애셋 형식 또는 요청 형식 (html/json) 또는 다른 점이 있습니까? –

+0

요청 형식 (json/xml) –