How to disable render view in zend framework 2?

前端 未结 6 611
臣服心动
臣服心动 2021-01-30 09:39

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 v

相关标签:
6条回答
  • 2021-01-30 10:11

    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;
    }   
    
    0 讨论(0)
  • 2021-01-30 10:12

    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

    0 讨论(0)
  • 2021-01-30 10:16
    • To disable your view :

      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;
      }
      
    0 讨论(0)
  • 2021-01-30 10:18

    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();
    
    0 讨论(0)
  • 2021-01-30 10:20
    ...
    use Zend\View\Model\JsonModel;
    
    public function myAction() {
        ...
    
        $view = new JsonModel($myArray);
        $view->setTerminal(true);
        return $view;
    }
    
    0 讨论(0)
  • 2021-01-30 10:26

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

    0 讨论(0)
提交回复
热议问题