Zend 框架 2 休息 api
Zend Framework 2 REST api
我有个小问题。我是 Zend Framework 2 的新手,我想用它创建一个 REST 服务。
我遵循了他的教程:http://www.slideshare.net/mikestowe/building-a-rest-api-with-zend-framework-2
问题是我收到一条错误消息,但我在 google 上的任何地方都找不到它。
致命错误:Application\Controller\UserController::setEventManager() 的声明必须与 C:\ 中的 Zend\EventManager\EventManagerAwareInterface::setEventManager(Zend\EventManager\EventManagerInterface $eventManager) 兼容wamp\www\zf2rest\module\Application\src\Application\Controller\UserController.php 第 15
行
这是我的用户控制器:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class UserController extends AbstractRestfulController {
protected $collectionOptions = array('GET', 'POST');
protected $resourceOptions = array('GET', 'POST', 'DELETE');
protected function _getOptions() {
if($this->params->fromRoute('id', false)) {
// we have an id, return specific item
return $this->resourceOptions;
}
//no ID, return collection
return $this->collectionOptions;
}
public function options() {
$response = $this->getResponse();
//If in Options Array, Allow
$response->getHeaders()
->addHeaderLine('Allow', implode(',', $this->_getOptions()));
//return Response
return $response;
}
public function setEventManager(EventManagerInterface $events) {
//events propery defined in AbstractController
$this->events = $events;
//Register the listener and callback method with a priority if 10
$events->attach('dispatch', array($this, 'checkOptions'), 10);
}
public function checkOptions($e) {
if(in_array($e->getRequest()->getMethod(), $this->_getOptions())) {
//Method Allowed, Nothing to do
return;
}
//Method Not Allowed
$response = $this->getResponse();
$response->setStatusCode(405);
return $response;
}
}
有人知道解决这个问题的方法吗?
非常感谢!
接口强制 setEventManager::$events 是 class 实现接口 Zend\EventManager\EventManagerInterface;但是你已经打破了这个,因为 EventManagerInterface 只是完全限定的 class 名称的别名。
您可以通过使用完全限定的 class 名称来避免这种情况
public function setEventManager(\Zend\EventManager\EventManagerInterface $events);
或者,保留您已有的,并在 class.
的顶部添加一个名称空间别名
<?php
namespace Api/Controller;
use Zend\EventManager\EventManagerInteface;
同样重要的是确保调用父 setEventManager() 否则所有默认配置都会过载,看看还设置了什么。
public function setEventManager(EventManagerInterface $events)
{
parent::setEventManager($events);
$events->attach('dispatch', array($this, 'checkOptions'), 10);
}
我有个小问题。我是 Zend Framework 2 的新手,我想用它创建一个 REST 服务。
我遵循了他的教程:http://www.slideshare.net/mikestowe/building-a-rest-api-with-zend-framework-2
问题是我收到一条错误消息,但我在 google 上的任何地方都找不到它。
致命错误:Application\Controller\UserController::setEventManager() 的声明必须与 C:\ 中的 Zend\EventManager\EventManagerAwareInterface::setEventManager(Zend\EventManager\EventManagerInterface $eventManager) 兼容wamp\www\zf2rest\module\Application\src\Application\Controller\UserController.php 第 15
行这是我的用户控制器:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class UserController extends AbstractRestfulController {
protected $collectionOptions = array('GET', 'POST');
protected $resourceOptions = array('GET', 'POST', 'DELETE');
protected function _getOptions() {
if($this->params->fromRoute('id', false)) {
// we have an id, return specific item
return $this->resourceOptions;
}
//no ID, return collection
return $this->collectionOptions;
}
public function options() {
$response = $this->getResponse();
//If in Options Array, Allow
$response->getHeaders()
->addHeaderLine('Allow', implode(',', $this->_getOptions()));
//return Response
return $response;
}
public function setEventManager(EventManagerInterface $events) {
//events propery defined in AbstractController
$this->events = $events;
//Register the listener and callback method with a priority if 10
$events->attach('dispatch', array($this, 'checkOptions'), 10);
}
public function checkOptions($e) {
if(in_array($e->getRequest()->getMethod(), $this->_getOptions())) {
//Method Allowed, Nothing to do
return;
}
//Method Not Allowed
$response = $this->getResponse();
$response->setStatusCode(405);
return $response;
}
}
有人知道解决这个问题的方法吗?
非常感谢!
接口强制 setEventManager::$events 是 class 实现接口 Zend\EventManager\EventManagerInterface;但是你已经打破了这个,因为 EventManagerInterface 只是完全限定的 class 名称的别名。
您可以通过使用完全限定的 class 名称来避免这种情况
public function setEventManager(\Zend\EventManager\EventManagerInterface $events);
或者,保留您已有的,并在 class.
的顶部添加一个名称空间别名<?php
namespace Api/Controller;
use Zend\EventManager\EventManagerInteface;
同样重要的是确保调用父 setEventManager() 否则所有默认配置都会过载,看看还设置了什么。
public function setEventManager(EventManagerInterface $events)
{
parent::setEventManager($events);
$events->attach('dispatch', array($this, 'checkOptions'), 10);
}