2012-02-08 29 views
20

Tôi đang phải vật lộn với việc vô hiệu hóa xem trong ZF2 $this->_helper->viewRenderer->setNoRender(); or (true) không có may mắn như nó luôn luôn nói rằng cóvô hiệu hóa tầm nhìn với hành động trong ZF2

PHP Fatal error: Call to a member function setNoRender() on a non-object in ../module/Location/src/Location/Controller/LocationController.php on line 190 

Trả lời

61

Để tắt chế độ xem hoàn toàn, từ bên trong một hành động điều khiển, bạn nên trả về một đối tượng Response:

<?php 

namespace SomeModule\Controller; 

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

class SomeController extends ActionController 
{ 
    public function someAction() 
    { 
     $response = $this->getResponse(); 
     $response->setStatusCode(200); 
     $response->setContent("Hello World"); 
     return $response; 
    } 
} 

Để tắt bố cục và chỉ hiển thị mẫu mô hình chế độ xem của hành động này, bạn sẽ làm điều này:

public function anotherAction() 
{ 
    $result = new ViewModel(); 
    $result->setTerminal(true); 

    return $result; 
} 
+0

cảm ơn bạn rất nhiều! :) – ufk

-1

$this->_helper không có sẵn trong ZF2 nhưng để vô hiệu hóa một cái nhìn bạn có thể làm:

$this->broker("ViewRenderer")->setNoRender(); 

hoặc

$this->broker->load("ViewRenderer")->setNoRender(); 
+0

Xin lỗi tôi đã cố gắng này, nhưng luôn luôn có được ** không thể xác định vị trí lớp liên kết với "viewrenderer" ** lỗi – mahi

+0

Cũng thấy điều này không làm việc - một trong hai phương pháp –

4

Các ZF2 là nặng nề đang được phát triển và không có guar antee có thể được thực hiện theo cách nó hoạt động ngay bây giờ, sẽ là cách nó hoạt động khi ZF2 đạt đến trạng thái ổn định.

Tuy nhiên, lớp xem mới từ Zend \ Mvc mới được hợp nhất, cung cấp tùy chọn trả về các kiểu xem với thông tin liên quan đến chế độ xem để hiển thị chế độ xem. Để tắt hiển thị chế độ xem, bạn có thể cắt ngắn việc gửi đi bằng cách trả lại trực tiếp phản hồi, vì vậy chế độ xem không được hiển thị.

public function somethingAction() 
{ 
    // Do some intelligent work 

    return $this->getResponse(); 
} 
0

Chỉ cần trở lại '' trong Phương pháp và nó sẽ không tự động load View mẫu

public function goAction() 
{ 
    return ''; 
} 
0

public function indexAction() {

$news = $this->em->getRepository('Admin\Model\News'); 
    foreach ($news->findAll() as $new) { 


     $res = $this->getResponse()->setContent($new->toXml()); 
    } 





    return $res; 

} 
3

Tôi tìm thấy một giải pháp đơn giản cho disable bố trí. Trong tôi ajaxAction

public function ajaxAction() 
{ 
    if ($this->getRequest()->isXmlHttpRequest()) { 

      $this->layout('layout/ajax-layout'); 

    } 
} 

Và trong \module\Application\view\layout\ajax-layout.phtml

<?php echo $this->content; ?> 
3

tôi sẽ nói chỉ vô hiệu hóa việc bố trí chỉ

$viewModel = new ViewModel(); 
$viewModel->setTerminal(true); 

return $viewModel; 

và echo json bạn vào xem các file của bạn ...

0
public function testAction() 
{ 
    return false; 
} 

si trả về sai.

0

Bạn có thể làm như vậy bằng cách sử dụng mô hình bảng điều khiển hoặc tự động thực thi.

<?php 

namespace SomeModule\Controller; 

use Zend\Mvc\Controller\ActionController; 
use Zend\View\Model\ConsoleModel; // if use ConsoleMode 
use Zend\View\Model\JsonModel; // if use JSON 

class SomeController extends ActionController 
{ 
    public function someAction() { 

     return new ConsoleModel(array(
     'message' => 'Hello World', 
    )); 

    } 
    // Json Method 
    public function jsonAction() { 

     return new JsonModel(array(
     'message' => 'Hello World', 
    )); 

    } 

    // This is really exaggerated, but it is quite effective. 

    public function killAction() { 
     echo 'Hello World'; 
     exit; 
    } 
} 

Theo quan điểm sử dụng: some.phtml

<?php 
echo $message; 

json.phtml

<?php 
echo $message; 
Các vấn đề liên quan