我正在应用一个关于使用 ZF2 rest api 创建一个 restful 应用程序的教程。
我遇到了一个奇怪的问题,除了create($data)
public function create($data)
{
$data['id'] = 0;
$form = new AlbumForm();
$album = new Album();
$form->setInputFilter($album->getInputFilter());
$form->setData($data);
if ($form->isValid()) {
$album->exchangeArray($data);
$id = $this->getAlbumTable()->saveAlbum($album);
return $this->get($id);
} else {
return new JsonModel(array("data" => 0 );
}
}
我在 Firefox 中使用了 restful 插件,数据看起来像这样
artist=testName&title=testTitle
I traced the problem and figured that the variable $data
always empty and does not store any value. On the other hand, when I use update($id, $data)
every thing work fine
public function update($id, $data)
{
$data['id'] = $id;
$album = $this->getAlbumTable()->getAlbum($id);
$form = new AlbumForm();
$form->bind($album);
$form->setInputFilter($album->getInputFilter());
$form->setData($data);
if ($form->isValid()) {
$id = $this->getAlbumTable()->saveAlbum($form->getData());
}
return new JsonModel(array('data' => $data));
}
So could I have some suggestions?
Update one :
here is the module.config.php
file to see if there is a mistake in it or not
<?php
return array(
'controllers' => array(
'invokables' => array(
'AlbumRest\Controller\AlbumRest' => 'AlbumRest\Controller\AlbumRestController',
),
),
// The following section is new` and should be added to your file
'router' => array(
'routes' => array(
'album-rest' => array(
'type' => 'Segment',
'options' => array(
'route' => '/album-rest[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'AlbumRest\Controller\AlbumRest',
),
),
),
),
),
'view_manager' => array(
'strategies' => array(
'ViewJsonStrategy',
),
),
);
Update two :
the problem was from the Firefox RestfulClient. I installed the postman and it worked perfectly.