disabling view with in action in ZF2

前端 未结 9 608
滥情空心
滥情空心 2020-12-24 03:10

I am struggling with disabling view in ZF2 $this->_helper->viewRenderer->setNoRender(); or (true) with no luck as it always says there



        
相关标签:
9条回答
  • 2020-12-24 03:23

    I found a simple solution for disable layout. In my ajaxAction

    public function ajaxAction()
    {   
         if ( $this->getRequest()->isXmlHttpRequest() ) {
    
              $this->layout( 'layout/ajax-layout' );
    
         }
    }
    

    And in \module\Application\view\layout\ajax-layout.phtml

    <?php echo $this->content; ?>
    
    0 讨论(0)
  • 2020-12-24 03:23

    $this->_helper is not available in ZF2 but to disable a view you can do :

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

    or

    $this->broker->load("ViewRenderer")->setNoRender();
    
    0 讨论(0)
  • 2020-12-24 03:24

    I would say just disabled the layout only

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

    and echo your json into your view files...

    0 讨论(0)
  • 2020-12-24 03:28

    public function indexAction() {

        $news = $this->em->getRepository('Admin\Model\News');
        foreach ($news->findAll() as $new) {
    
    
            $res = $this->getResponse()->setContent($new->toXml());
        }
    
    
    
    
    
        return $res;
    
    }
    
    0 讨论(0)
  • 2020-12-24 03:29

    The ZF2 is heavily under development and no guarantee can be made the way it works now, will be the way it works when ZF2 reaches a stable state.

    However, the new view layer from Zend\Mvc is recently merged, which gives the option to return view models with view related information to render views. To disable view rendering, you can short-cut dispatching by returning a response directly, so the view is not rendered at all.

    public function somethingAction () 
    {
        // Do some intelligent work
    
        return $this->getResponse();
    }
    
    0 讨论(0)
  • 2020-12-24 03:37

    To disable the view completely, from within a controller action, you should return a Response object:

    <?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;
        }   
    }
    

    To disable the layout and just render this action's view model template you would do this:

    public function anotherAction()
    {
        $result = new ViewModel();
        $result->setTerminal(true);
    
        return $result;
    }
    
    0 讨论(0)
提交回复
热议问题