【问题标题】:disabling view with in action in ZF2在 ZF2 中禁用视图
【发布时间】:2012-02-08 12:59:16
【问题描述】:

我正在努力禁用 ZF2 $this->_helper->viewRenderer->setNoRender(); or (true) 中的视图,但没有运气,因为它总是说在那里

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

【问题讨论】:

    标签: zend-framework2


    【解决方案1】:

    要完全禁用视图,在控制器操作中,您应该返回一个 Response 对象:

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

    要禁用布局并仅渲染此操作的视图模型模板,您可以这样做:

    public function anotherAction()
    {
        $result = new ViewModel();
        $result->setTerminal(true);
    
        return $result;
    }
    

    【讨论】:

      【解决方案2】:

      ZF2 正在大力开发中,不能保证它现在的工作方式,将是 ZF2 达到稳定状态时的工作方式。

      然而,来自 Zend\Mvc 的新视图层最近被合并,它提供了返回带有视图相关信息的视图模型以渲染视图的选项。要禁用视图渲染,您可以通过直接返回响应来进行快捷调度,这样视图根本不会被渲染。

      public function somethingAction () 
      {
          // Do some intelligent work
      
          return $this->getResponse();
      }
      

      【讨论】:

        【解决方案3】:

        我找到了一个禁用布局的简单解决方案。在我的ajaxAction

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

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

        <?php echo $this->content; ?>
        

        【讨论】:

          【解决方案4】:

          我会说只是禁用了布局

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

          并将您的 json 回显到您的视图文件中...

          【讨论】:

            【解决方案5】:

            只在方法中返回'',它不会自动加载视图模板

            public function goAction()
            {   
                return '';
            }
            

            【讨论】:

              【解决方案6】:

              公共函数 indexAction() {

                  $news = $this->em->getRepository('Admin\Model\News');
                  foreach ($news->findAll() as $new) {
              
              
                      $res = $this->getResponse()->setContent($new->toXml());
                  }
              
              
              
              
              
                  return $res;
              
              }
              

              【讨论】:

                【解决方案7】:
                public function testAction()
                {   
                    return false;
                }
                

                只需返回 false。

                【讨论】:

                  【解决方案8】:

                  您可以使用控制台模型执行此操作,也可以任意终止执行。

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

                  在视图中使用: 一些.phtml

                  <?php
                  echo $message;
                  

                  json.phtml

                  <?php
                  echo $message;
                  

                  【讨论】:

                    【解决方案9】:

                    $this-&gt;_helper 在 ZF2 中不可用,但您可以禁用视图:

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

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

                    【讨论】:

                    • 对不起,我已经尝试过了,但总是得到 unable to locate class associated with "viewrenderer" 错误
                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2016-03-12
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多