3

I have a ZF MVC app I created with composer create-project -sdev zendframework/skeleton-application my-application

I made a controller like the following.

class SomeController extends AbstractRestfulController { public function someAction() { $key = $this->params()->fromQuery('key'); if (empty($key)) { $this->response->setStatusCode(Response::STATUS_CODE_400); return new JsonModel([ 'status'=> 'Error', 'messages'=> [ 'key required' ], ]); } return $this->someService->getStringByKey($key)); } } 

I want it to return a content type of text/plain with a body of the results of SomeService::getStringByKey($key). Instead I get the error:

Zend\View\Renderer\PhpRenderer::render: Unable to render template "XXXXXXXXXX"; resolver could not resolve to a file `

How do I make the controller actions just return plain strings?

1 Answer 1

3

Well, you are very close :)

class SomeController extends AbstractRestfulController { /** * @return \Zend\Http\PhpEnvironment\Response */ public function someAction() { $string = $this->someService->getStringByKey($key)); $this->response->getHeaders()->addHeaderLine('Content-Type: text/plain'); return $this->response->setContent($string); } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Made an edit so the mime type would be set. I was hoping for a more cleaner solutions like a RawTextStrategy, but this will do for now.
Hm, yu can check existing Models and Strategy github.com/zendframework/zend-view/tree/master/src Strategy for raw plan text maybe would be a overengineering :) but anyhow interesting...
I'm working on a legacy app that uses Zend 1.10. This question came up when I tried to ask this for Zend 1.10, so I'll make a comment here for reference to my future self and others that may come across this, as I could not figure this out anywhere else (even in the Zend docs, which it must be there somewhere). The $this->response pattern was added after Zend 1.10. In Zend 1.10 $this->getResponse() must be called. The full example would be $this->getResponse()->setBody('something'); $this->getResponse()->sendResponse(); exit; Well, some headers must be set somewhere, too.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.