开发者

How to skip view/layout in a zf controller

开发者 https://www.devze.com 2023-01-09 18:07 出处:网络
I am making a controller that will be responsible for a bunch of actions and I don\'t 开发者_Go百科want to have to create a view file for each one, sometimes i just want to output strings.

I am making a controller that will be responsible for a bunch of actions and I don't 开发者_Go百科want to have to create a view file for each one, sometimes i just want to output strings.

I could just do echo 'Hello World'; die(); into the action. but is there a more correct way to do this?


Yes, in the controller you can disable view rendering like this:

$this->getHelper('viewRenderer')->setNoRender();

And you add whatever you like to the output like this:

$this->getResponse()->setBody('Hello Moak!');

There are other things you can do with the Response object too:

$r = $this->getResponse();
$r->setHeader('Content-type', 'text/html', true);
$r->setRawHeader('HTTP/1.1 200 OK');
$r->setHttpResponseCode(200);
$r->clearBody();
$r->setBody('<html><h1>Hello</h1></hello>');
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号