开发者

How to skip view/layout in a zf controller

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜