ZEST Framework 2 RESTful web service template error

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

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.

+4
source share
2 answers

You are right that this β€œrender” error is that it cannot find your presentation template, but the good news is that you do not need it:

Assuming your calm service returns JSON, try this at the bottom of your controller action:

 $result = new \Zend\View\Model\JsonModel($data_you_were_already_returning); return $result; 
+4
source

Enabling the ViewJsonStrategy function does not mean that responses will be automatically returned as JSON. This means that if you return JsonModel from your controller, JsonStrategy will intercept it and return JSON. ( http://zend-framework-community.634137.n4.nabble.com/Returning-JSON-for-404-and-for-exception-td4660236.html )

So you need to manually replace ViewModel with 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); } } 
+1
source

Source: https://habr.com/ru/post/1502898/


All Articles