0

I am new to Web service and i like to make a web service in Zend Framework 2 using AbstractRestfulController.. Can any one give me a simple example. May be with a single function.

Thanks.

Wilt
  • 41,477
  • 12
  • 152
  • 203
Vivek
  • 78
  • 7

1 Answers1

0

In your config.module.php

'controllers' => array(
    'invokables' => array(
        'My\Controller\ObjectController' => 'My\Controller\ObjectController'
    )
)

In your router

'object' => array(
    'type' => 'segment',
    'options' => array(
        'route' => '/objects[/:object_id]',
        'constraints' => array(
            'object_id' => '[0-9]*'
        ),
        'defaults' => array(
            'controller' => 'My\Controller\ObjectController'
        )
    )
),

Your controller

<?php

namespace My\Controller;

use Zend\Mvc\Controller\AbstractRestfulController;

class ObjectController extends AbstractRestfulController {

    public function get($id)
    {
        return new JsonModel( ... add Json representation of object with $id ... );
    }

    public function getList()
    {
        return new JsonModel( ... add Json representation of object collection ... );
    }


    ... add other methods that are needed ...

}

That is basically it

Wilt
  • 41,477
  • 12
  • 152
  • 203
  • Thank You, But http://samsonasik.wordpress.com/2012/10/31/zend-framework-2-step-by-step-create-restful-application/ Got this tutorial.. Very helpful.. I got some Doubts.. can you please tell me where do these two controllers are linked?? Also can you tell me where do i put the database connection model , in Restful Controller or AbstractAction Controller? – Vivek Aug 29 '14 at 08:48
  • They are linked to the route. You can get the data from the database in the methods in the controller or to prevent code duplication make a new method inside the controller and call it from the other methods. Whatever you prefer. – Wilt Aug 29 '14 at 09:40
  • Can you specify the Controller?? Restful Controller or AbstractAction Controller? – Vivek Aug 29 '14 at 11:17
  • You extend the `AbstractRestfulController` and add the methods in your own controller class. In this example the `ObjectController` – Wilt Aug 29 '14 at 12:14
  • What if the table is different on different function.. Do i need to create different classes?? or can i pass the table name ? – Vivek Sep 01 '14 at 05:00
  • ie. how can we create a restful service which deals with different tables?? pls help.. – Vivek Sep 01 '14 at 05:18