制作 Zend Framework 3 MVC 应用 return 一个简单的字符串
Making a Zend Framework 3 MVC app return a simple string
我有一个使用 composer create-project -sdev zendframework/skeleton-application my-application
创建的 ZF MVC 应用程序
我做了一个如下所示的控制器。
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));
}
}
我希望它 return 内容类型 text/plain 和结果主体 SomeService::getStringByKey($key)
。相反,我收到错误:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "XXXXXXXXXX"; resolver could not resolve to a file
`
如何使控制器动作只是 return 纯字符串?
嗯,你很亲近:)
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);
}
}
我有一个使用 composer create-project -sdev zendframework/skeleton-application my-application
我做了一个如下所示的控制器。
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));
}
}
我希望它 return 内容类型 text/plain 和结果主体 SomeService::getStringByKey($key)
。相反,我收到错误:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "XXXXXXXXXX"; resolver could not resolve to a file `
如何使控制器动作只是 return 纯字符串?
嗯,你很亲近:)
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);
}
}