Great idea behind this project. I've tried to integrate it with ZF2 and I'm having some problems (I use modules DoctrineModule and DoctrineORMModule) and existing entities.
I've was able to install Drest via composer and that went smooth, and further I was able to add Drest\Resources annotations so that part looks OK. Now I'm having problems with accessing the API via browser, while ZF throws a routing error.
EDIT: I am now trying with a ZF2 module for api/Drest with one controller, which will handle api routing. I'm not sure if this is the right approach but I guess I'll find that out soon. I was actually able to get Drest router to get executed, but it throws an error: "There are no routes configured to match this request path". I'm using dispatcher like this:
$drestManager->dispatch($this->getRequest(), new \Zend\Http\PhpEnvironment\Response()) Is there a way for Drest to automatically get parameters from getRequest?
Btw, currently ZF2 router looks like this:
'router' => array(
'routes' => array(
'apps' => array(
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => array(
'route' => $root_route,
'defaults' => array(
'controller' => 'Api\Controller\Apps',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[apps[/][:appId]]',
'constraints' => array(
'appId' => '[0-9]+',
),
'defaults' => array(
),
),
),
),
),
EDIT2: I was actually able to get this working in ZF2 by adding 3rd and 4th parameter to dispatch function. I was wondering if this approach is OK or is there a better more general way for doing this?
WORKING ZF2 Controller with Drest:
public function indexAction()
{
$id = (int) $this->params()->fromRoute('appId', 0);
$entityManager = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
$pathToEntities = array(__DIR__ . '/../../../../Application/src/Application/Entity');
$request = $this->getRequest();
$drestConfig = new \Drest\Configuration();
$drestConfig->setDetectContentOptions(array(
Configuration::DETECT_CONTENT_HEADER => 'Accept',
Configuration::DETECT_CONTENT_EXTENSION => true,
Configuration::DETECT_CONTENT_PARAM => 'format'
));
$drestConfig->setExposureDepth(1);
$drestConfig->setExposeRequestOption(Configuration::EXPOSE_REQUEST_PARAM_GET, 'expose');
$drestConfig->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); // use a different adapter for production
$drestConfig->setDebugMode(true);
$drestConfig->addPathsToConfigFiles($pathToEntities);
$evm = new Event\Manager();
//$evm->addEventListener(array('preServiceAction', 'postServiceAction', 'preRouting', 'postRouting', 'preDispatch', 'postDispatch'), new MyEvents\MyEvent());
$drestManager = \Drest\Manager::create($entityManager, $drestConfig, $evm);
$test = new \Zend\Http\PhpEnvironment\Response();
$viewModel = new ViewModel(array(
'data'=>$drestManager->dispatch($request, new \Zend\Http\PhpEnvironment\Response(), 'Application\Entity\Apps::get_apps', array('id' => $id)),
));
$viewModel->setTerminal(true);
return $viewModel;
}
EDIT3 - POST Request:
I also tried to implement POST functionality with similar dispatcher than above (just didn't pass in array with id), and I didn't have that much luck here. I'm getting an error
Requests to push data (PUT/POST/PATCH) can only be used on individual elements. Data collections cannot be pushed
stack trace:
Drest\Manager.php(310): Drest\Query\ExposeFields->configurePushRequest(NULL)
Manager.php(211): Drest\Manager->handlePushExposureConfiguration(Object(Drest\Mapping\RouteMetaData), Object(DrestCommon\Representation\Json))
...
Any idea what I'm doing wrong?