2012-09-09 4 views
4

젠드 프레임 워크 2보기에서 많은 시간을 내 데이터가 안전한지 확인하기 위해 $this->escapeHtml()으로 전화 할 것입니다. 이 동작을 블랙리스트에서 허용 목록으로 전환 할 수있는 방법이 있습니까?Zend Framework에서 자동으로 변수를 이스케이프하는 방법 2보기

추신 : Padraic Brady의 기사에서 automatic escaping is a bad idea을 읽어보십시오. 추가 생각?

+0

블랙리스트에서 화이트리스트로 전환하는 행동은 무엇을 의미합니까? escapeHTML이 호출 되더라도 텍스트를 이스케이프하지 않는다는 것을 의미합니까? 텍스트를 이스케이프하지 않도록 함수를 호출 할 수 없습니다. –

+0

지금 바로 변수를 블랙리스트에 올리십시오. 즉, "보안"할 항목을 선택하십시오. 나는 그 동작이 반대로 이루어 지길 원한다. 즉, 선택 (raw) 된 (이스케이프되지 않은) 출력을 선택한다. –

답변

4

변수가 할당되면 데이터를 이스케이프 처리하는 고유 한 ViewModel 클래스를 작성할 수 있습니다.

public function fooAction() 
{ 
    return new EscapeViewModel(array(
     'foo' => '<i>bar</i>' 
    )); 

    //Turn off auto-escaping: 
    return new EscapeViewModel(array(
     'foo' => '<i>bar</i>' 
    ),['auto_escape' => false]); 
} 

질문 : 내가 감사하겠습니다가이처럼 사용할 수있는 컨트롤러에서

namespace Application\View\Model; 

use Zend\View\Model\ViewModel; 
use Zend\View\Helper\EscapeHtml; 

class EscapeViewModel extends ViewModel 
{ 
/** 
* @var Zend\View\Helper\EscapeHtml 
*/ 
protected $escaper = null; 

/** 
* Proxy to set auto-escape option 
* 
* @param bool $autoEscape 
* @return ViewModel 
*/ 
public function autoEscape($autoEscape = true) 
{ 
    $this->options['auto_escape'] = (bool) $autoEscape; 
    return $this; 
} 

/** 
* Property overloading: get variable value; 
* auto-escape if auto-escape option is set 
* 
* @param string $name 
* @return mixed 
*/ 
public function __get($name) 
{ 
    if (!$this->__isset($name)) { 
     return; 
    } 

    $variables = $this->getVariables(); 
    if($this->getOption('auto_escape')) 
     return $this->getEscaper()->escape($variables[$name]); 
    return $variables[$name]; 
} 


/** 
* Get instance of Escaper 
* 
* @return Zend\View\Helper\EscapeHtml 
*/ 
public function getEscaper() 
{ 
    if (null === $this->escaper) { 
     $this->escaper = new EscapeHtml; 
    } 

    return $this->escaper; 
} 
} 

다음과 같이 훔치는 주석에

1

덕분에, 나는 ZF2 뷰 모델을 확장 soemebody가 의견을 제시한다면, 이것이 최선의 관행이라면 더 좋고 ecp가 더 좋은가? 더 효율적이고 자원 절약적인 방법?

관련 문제