您好,当我尝试访问 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
我假设这表明应用程序找不到所需的视图文件。我从本教程开始。我已经搜索了我的问题的答案,并且我发现了其他一些有类似问题的人,但我目前还没有找到我正在寻找的答案,因为我仍然有一个错误。我对 Zend Framework 2 比较陌生,所以对于更有经验的人来说,这可能是一个简单的问题。
以下是我迄今为止在路由和视图管理器策略方面所做的工作:
模块.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
我想知道是否需要此视图文件,它是否位于正确的位置并正确命名?
对于此错误的任何其他帮助将不胜感激。如果有帮助,我很乐意提供更多信息。
谢谢你。