Wednesday 2 January 2013

Zend Framework 2: Disable layout rendering

In ZF1.x we have method for disabling layout rendering that looks like:

$this->_helper->layout()->disableLayout(); 

Of course in ZF2 this method doesn't work but there are some other ways how it is possible to do:

1. Set viewModel as standalone model in controller action:

public function someAction() {
    $viewModel = new ViewModel(array(
        'foo' => 'bar'
    ));

    $viewModel->setTerminal(true);

    return $viewModel;
}

2. Create empty layout and use it in controller action:

Create almost empty layout: module/MyModule/view/layout/empty.phtml with only content:
<?php echo $this->content; ?>

Then use this layout in controller action:
$this->layout('layout/empty');

3. Use response object for output content:

$response = $this->getResponse();
$response->setContent("Some content"); 
return $response;

That's all. May be there are some other ways to do this. As for me I prefer the first way, it looks simple and clean.

17 comments:

  1. Howdy! This post could not be written any better! Reading through this post reminds me of my good old room mate! He always kept talking about this. I will forward this post to him. Fairly certain he will have a good read. Thank you for sharing!
    How I can Create YouTube Channel?

    ReplyDelete
  2. This is very essential part in Zend Frame work process to disable the layout rendering.I am very grateful to this blog for explaining this process with example.
    Web Design Companies | Website Design Companies

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene.ad agencies nyc

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Hi,

    good information

    Web application development services will successfully change the execution of your online business So, hire our skilled Ecommerce website company to get customized applications for your business.

    Best Website Designing and Development Company
    "
    Ecommerce Website Development Company
    "
    Zend Framework

    ReplyDelete
  7. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
    Devops Training courses
    Devops Training in Bangalore

    ReplyDelete
  8. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    python Course in Pune
    python Course institute in Chennai
    python Training institute in Bangalore

    ReplyDelete
  9. It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete
  10. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck…

    Join Cloud Computing Training in Bangalore at Softgen Infotech. Learn from Certified Professionals with 10+ Years of experience in Cloud Computing. Get 100% Placement Assistance. Placements in MNC after successful course completion.

    ReplyDelete
  11. Great post. keep sharing such a worthy information.
    AWS Training institute in Chennai

    ReplyDelete