2012-08-24 20 views
16

컨트롤러 동작에서 기본 뷰가 아닌 다른 뷰를 렌더링하는 방법. 기본적으로보기 폴더에서 작업과 동일한보기를 찾으려고하지만 뷰어 폴더에서 제어 도구 동작을 위해 다른보기를 사용할 수 있습니다. 우리는이 작업을 수행 할 수ZF2의 컨트롤러 동작에서 다른 뷰를 렌더링하는 방법

는 다음과 같이 ZF1 $this->_helper->viewRenderer('foo');

사람이 어떻게 Zendframework 2 위 달성을 알 수 있습니까?

우리는 내가 변경하는 방법을 모르는

$response = $this->getResponse(); 
     $response->setStatusCode(200); 
     $response->setContent("Hello World"); 
     return $response; 

사용하여 뷰를 사용할 수 있습니다/zf2에서 다른 뷰를 렌더링합니다.

답변

44

는 거의 모든 시나리오를 덮고 akrabat

public function abcAction() 
{ 
    $view = new ViewModel(array('variable'=>$value)); 
    $view->setTemplate('module/controler/action.phtml'); // path to phtml file under view folder 
    return $view; 
} 

감사를 사용하여 수행 할 수 있습니다.

+1

HTTP에서 노선과 view_manager를 구성하는 것을 잊지 : // zf2test.akrabat.com/ – Developer

+1

https://github.com/akrabat/ZF2TestApp/blob/master/module/Application/config/module.config.php#L78 – Developer

+0

+1, 완벽한 답변 !!! – SagarPPanchal

2

Zend Framewor 2의 내 솔루션은 간단합니다. 인덱스 작업의 경우 parrent :: indexAction() 생성자 bcs 확장자 Zend \ Mvc \ Controller \ AbstractActionController을 호출하는 것이 좋습니다. 아니면 indexAction에 array()을 반환하십시오. ZF는 리턴되어야하는 것을 인덱스로 정의한 index.pthml을 원자 적으로 반환합니다.

반환 새로운 ViewManager()이 같은 반환 배열()이다

<?php 

namespace Test\Controller; 

use Zend\Mvc\Controller\AbstractActionController, 
    Zend\View\Model\ViewModel; 

// Or if u write Restful web service then use RestfulController 
// use Zend\Mvc\Controller\AbstractRestfulController 

class TestController extends AbstractActionController 
{ 
    /* 
    * Index action 
    * 
    * @return main index.phtml 
    */ 

    public function indexAction() 
    { 
      parent::indexAction(); 

      // or return new ViewModel(); 
      // or much simple return array(); 
    } 

    /* 
    * Add new comment 
    * 
    * @return addComment.phtml 
    */ 

    public function addAction() 
    { 
     $view = new ViewManager(); 
     $view->setTemplate('test/test/addComment.phtml'); // module/Test/view/test/test/ 

     return $view; 
    } 

망가 모듈/설정/module_config

'view_manager' => array(
     'template_path_stack' => array(
      'Test' => __DIR__ . '/../view', 
     ), 
    ), 
관련 문제