Making a Zend Framework 3 MVC app return a simple string

841 Views Asked by At

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

There are 1 best solutions below

3
tasmaniski On BEST ANSWER

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