我在Zend Framework 2.2.2项目中尝试访问RESTful Web服务端点时遇到错误。我正在创建一个名为V1的模块,我收到以下错误:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "v1/collateral/get-list"; resolver could not resolve to a file
我认为这表明应用程序无法找到所需的视图文件。我从this tutorial开始。我已经搜索了我的问题的答案,我发现其他一些有类似问题但我还没有找到我正在寻找的答案,因为我仍然有错误。我对Zend Framework 2比较陌生,所以对于经验丰富的人来说这可能很容易。
以下是我到目前为止关于路由和视图管理器策略所做的事情:
module.config.php:
return array(
'router' => array(
'routes' => array(
'collateral' => array(
'type' => 'segment',
'options' => array(
'route' => '/v1/collateral[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'V1\Controller\Collateral',
),
),
),
),
),
'controllers' => array(
'invokables' => array(
'V1\Controller\Collateral' => 'V1\Controller\CollateralController',
),
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
),
);
这是我的控制器代码
CollateralController.php
namespace V1\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use V1\Model\Collateral;
//use V1\Form\CollateralForm;
use V1\Model\CollateralTable;
use Zend\View\Model\JsonModel;
class CollateralController extends AbstractRestfulController
{
protected $collateralTable;
public function getList()
{
$results = $this->getCollateralTable()->fetchAll();
$data = array();
foreach($results as $result) {
$data[] = $result;
}
return array('data' => $data);
}
public function get($id)
{
# code...
}
/*public function create($data)
{
# code...
}
public function update($id, $data)
{
# code...
}
public function delete($id)
{
# code...
}*/
public function getCollateralTable()
{
if (!$this->collateralTable) {
$sm = $this->getServiceLocator();
$this->collateralTable = $sm->get('V1\Model\CollateralTable');
}
return $this->collateralTable;
}
}
为了更好地衡量,我的 Module.php 文件
namespace V1;
// Add these import statements:
use V1\Model\Collateral;
use V1\Model\CollateralTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array(
'factories' => array(
'V1\Model\CollateralTable' => function($sm) {
$tableGateway = $sm->get('CollateralTableGateway');
$table = new CollateralTable($tableGateway);
return $table;
},
'CollateralTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Collateral());
return new TableGateway('collateral', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
虽然我不确定基于我在教程中阅读的内容,但如果我需要它,我创建了以下空视图文件:
\module\V1\view\v1\collateral\get-list.phtml
我想知道这个视图文件是否是必需的,是否在正确的位置并正确命名?
对此错误的任何其他帮助将不胜感激。如果它有用,我很乐意提供更多信息。
谢谢。
答案 0 :(得分:4)
你说“渲染错误”是因为它找不到你的视图模板是正确的,但好消息是你不一定需要它:
假设您的restful服务正在返回JSON,请在控制器操作的底部尝试:
$result = new \Zend\View\Model\JsonModel($data_you_were_already_returning);
return $result;
答案 1 :(得分:1)
启用ViewJsonStrategy并不意味着响应 自动返回为JSON。这意味着如果你回来了 来自你的控制器的JsonModel,JsonStrategy将拦截它 返回JSON。 (http://zend-framework-community.634137.n4.nabble.com/Returning-JSON-for-404-and-for-exception-td4660236.html)
因此,您需要手动将ViewModel替换为JsonModel
RestApi\Module.php
public function onBootstrap($e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach('render', array($this, 'registerJsonStrategy'), 100);
}
/**
* @param \Zend\Mvc\MvcEvent $e The MvcEvent instance
* @return void
*/
public function registerJsonStrategy(\Zend\Mvc\MvcEvent $e) {
$matches = $e->getRouteMatch();
$controller = $matches->getParam('controller');
if (false === strpos($controller, __NAMESPACE__)) {
// not a controller from this module
return;
}
// Potentially, you could be even more selective at this point, and test
// for specific controller classes, and even specific actions or request
// methods.
// Set the JSON model when controllers from this module are selected
$model = $e->getResult();
if($model instanceof \Zend\View\Model\ViewModel)
{
$newModel = new \Zend\View\Model\JsonModel($model->getVariables());
//$e->setResult($newModel);
$e->setViewModel($newModel);
}
}