Zend Framework 2: Auto disable layout for ajax calls

前端 未结 8 1791
终归单人心
终归单人心 2020-12-16 13:26

An AJAX request to one of my controller actions currently returns the full page HTML.

I only want it to return the HTML (.phtml contents) for that particular action.

8条回答
  •  不思量自难忘°
    2020-12-16 13:55

    Indeed the best thing would be to write another Strategy. There is a JsonStrategy which can auto-detect the accept header to automatically return Json-Format, but as with Ajax-Calls for fullpages, there it's good that it doesn't automatically do things, because you MAY want to get a full page. Above mentioned solution you mentioned would be the quick way to go.

    When going for full speed, you'd only have one additional line. It's a best practice to always return fully qualified ViewModels from within your controller. Like:

    public function indexAction() 
    {
        $request   = $this->getRequest();
        $viewModel = new ViewModel();
        $viewModel->setTemplate('module/controller/action');
        $viewModel->setTerminal($request->isXmlHttpRequest());
    
        return $viewModel->setVariables(array(
             //list of vars
        ));
    }
    

提交回复
热议问题