0

I was applying a tutorial about creating a restful application using ZF2 rest api.

I faced a weird problem, all of the rest action work perfectly except 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 );
    }
} 

I used restful plugin in Firefox and the data look like that

artist=testName&title=testTitle

I traced the problem and figured that the variable $dataalways 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.

Community
  • 1
  • 1
Mohd Alomar
  • 953
  • 1
  • 13
  • 30
  • do you get any errors on create function? – dixromos98 Sep 29 '14 at 07:41
  • @dixromos98 No, Status Code: 200 OK Connection: Keep-Alive Content-Length: 18 Content-Type: application/json; charset=utf-8 Date: Mon, 29 Sep 2014 07:54:31 GMT Keep-Alive: timeout=5, max=100 but when I return or save the content $data it show it as empty. if ($form->isValid()) evaluate as false, I really don't know what why $data doesn't receive anything it may be a problem with the routes I will update the post – Mohd Alomar Sep 29 '14 at 07:58
  • can you also display the data you pass in the create function? – dixromos98 Sep 29 '14 at 08:53
  • artist=testName&title=testTitle – Mohd Alomar Sep 29 '14 at 08:59
  • okay so that is the problem. The reason it evaluates false is because the setData method expects an array. so you should convert the data from artist=testName&title=testTitle to data = array(artist=>testName, title=>testTitle) – dixromos98 Sep 29 '14 at 09:02
  • Basically the $data should validate against your form fields. You can find more info here http://framework.zend.com/manual/2.0/en/modules/zend.form.quick-start.html – dixromos98 Sep 29 '14 at 09:03
  • Thanks, I updated the post, the problem was from the plugin I tested postman restful extension and it work. – Mohd Alomar Sep 29 '14 at 09:04

0 Answers0