disabling view with in action in ZF2

2019-01-31 12:58发布

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

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

9条回答
干净又极端
2楼-- · 2019-01-31 13:33

I would say just disabled the layout only

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

return $viewModel;

and echo your json into your view files...

查看更多
Ridiculous、
3楼-- · 2019-01-31 13:39

You can do so using the console model, or kill execution arbitrarily.

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

In view use: some.phtml

<?php
echo $message;

json.phtml

<?php
echo $message;
查看更多
4楼-- · 2019-01-31 13:45

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();
}
查看更多
来,给爷笑一个
5楼-- · 2019-01-31 13:46

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;
}
查看更多
Fickle 薄情
6楼-- · 2019-01-31 13:47

$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();
查看更多
你好瞎i
7楼-- · 2019-01-31 13:49

Just return '' in the Method and it will not autoload the View template

public function goAction()
{   
    return '';
}
查看更多
登录 后发表回答