Hey. I get an error when trying to access the endpoint of a RESTful web service in my Zend Framework 2.2.2 project. I create a module called V1 and I get the following error:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "v1/collateral/get-list"; resolver could not resolve to a file
I assume this means that the application cannot find the required view file. I started with this tutorial . I was looking for the answer to my question, and I found some others with a similar problem, but I did not find the answer I'm looking for at this moment, because I still have an error. I'm relatively new to Zend Framework 2, so this might be easy for someone more experienced.
Here is what I have done so far regarding the routing and strategy of the view manager:
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', ), ),
);
Here is my controller code
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) {
And for good measure, here is my Module.php file
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); }, ), ); } }
Although I'm not sure, based on what I read in the tutorial, if I need it or not, I created the following empty file of the form:
\module\V1\view\v1\collateral\get-list.phtml
I am wondering if this view file is needed and is it in the right place and correctly named?
Any further help with this error would be greatly appreciated. I am happy to provide additional information if this is helpful.
Thanks.