zend framework里面如何禁止自动渲染?

这个问题网上的答案都是抄来抄去的。下面的答案才是正解好吗。。网上的答案可能都是抄1.0版本的。


I want to use some ajax, but I don't know how to use function as the same as setNoRender() in zend framework 2 to disable for render view.

How to disable rendering view in zend framework 2?

share improve this question
 
1 
Another similar question with a more detailed reply. See stackoverflow.com/a/9870758/951920 –  Terre Porter Oct 1 '12 at 16:42
 

6 Answers

up vote 53 down vote accepted
  • To disable your view :【这个是正解啊,完全不要渲染就用return $this->repsonse..看下面的解释。

    public function myactionAction()
    {
        // your code here ...
        return false;
    }

"return false" disables the view and not the layout! why? because the accepted types are:

  • ViewModel
  • array
  • null

so "false" disable the view.

  • To disable layout and view, return a response object:

    public function myactionAction()
    {
        // your code here ...
        return $this->response;
    }
  • To disable layout:

    public function myactionAction()
    {
        // your code here ...
        $view = new ViewModel();
        $view->setTerminal(true);
        return $view;
    }
share improve this answer
 
 
Its not $view->setTerminate(true); Its $view->setTerminal(true); –  Beniston  Jan 15 '13 at 10:22 
 
Great answer. I updated the method call as Beniston rightly pointed out a typo. –  David Caunt  Mar 5 '13 at 13:50
 
Most of my action need to disable layout. Is there a way to set this from the view strategy, to avoid repeating code the above code? –  albanx  Apr 19 '13 at 8:44
4 
This is a great answer but why does returning a Response object disable the layout and view? That's strange.–  Quolonel Questions  Jul 14 '13 at 21:33

If you're using JSON, then look at the view's JsonStrategy and return a JsonModel from you controller. See this article.

Alternatively, you can return an Response from your controller and the whole view layer is skipped:

public function testAction()
{
    $response = $this->getResponse();
    $response->setStatusCode(200);
    $response->setContent('foo');
    return $response;
}   
share improve this answer
 
 
that 's very helpful. Thanks a lot –  Tai T  Sep 8 '12 at 18:27
1 
Just a quick notice. In Zend Framework 2.2 the $response->setBody('foo') is $response->setContent('foo'); –  John Skoumbourdis  Jul 6 '13 at 5:52

Proper and simple solution to do this

public function testAction()
{
    $data = array(
        'result' => true,
        'data' => array()
    );
    return $this->getResponse()->setContent(Json::encode($data));
}

Details: http://cmyker.blogspot.com/2012/11/zend-framework-2-ajax-return-json.html

share improve this answer
 
1 
For a REALLY proper solution, set the header first: public function testAction{ $response->getHeaders()->addHeaderLine('Content-Type', 'application/json'); return $response->setContent(JSON::encode($data));} –  Katya S  Oct 8 '14 at 13:52

I found some answer.

Though $this->layout()->getLayout() returns the name/path of the newly selected layout... The layout does not change with any of the following commands...

within a controller

$this->getLocator()->get('view')->layout()->setLayout('layouts/ajax.phtml');
$this->getLocator()->get('view')->layout()->setLayout('ajax');
$this->getLocator()->get('view')->layout()->disableLayout();

within a view PHTML file

$this->layout()->setLayout('layouts/ajax.phtml');
$this->layout()->setLayout('ajax');
$this->layout()->disableLayout();
share improve this answer
 

$view = new ViewModel(); $view->setTerminate(true);

share improve this answer
 
 
It's $view->setTerminal(true); actually. Maybe the API changed over time –  paul.ago  Aug 3 '15 at 18:34
...
use Zend\View\Model\JsonModel;

public function myAction() {
    ...

    $view = new JsonModel($myArray);
    $view->setTerminal(true);
    return $view;
}
share improve this answer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值