4

I can't get this simple example working with a custom action.

I had to move it form here How can I create a simple non-CRUD resource action with Drupal 7 / Services 3? to an own question.

The example looks like this for CRUD retrieve:

function mymod_services_resources() {
 $api = array(
   'test' => array(
     'operations' => array(
       'retrieve' => array(
         'help' => 'Drupal 7 Services 3 test',
         'callback' => '_mymod_test',
         'access callback' => 'user_access',
         'access arguments' => array('access content'),
         'access arguments append' => FALSE,
         'args' => array(
           array(
             'name' => 'fn',
             'type' => 'string',
             'description' => 'Function to perform',
             'source' => array('path' => '0'),
             'optional' => TRUE,
             'default' => '0',
           ),
         ),
       ),
     ),
   ),
 );
  return $api;
}

function _mymod_test($fn) {
  return "Test is working";
}

This works fine and returns for http://myhost.dev/endpoint/test/retrieve

Test is working

But when i try a custom action like this

function mymod_services_resources() {
 $api = array(
   'test' => array(
     'actions' => array(
       'custom' => array(
         'help' => 'Drupal 7 Services 3 test',
         'callback' => '_mymod_test',
         'access callback' => 'user_access',
         'access arguments' => array('access content'),
         'access arguments append' => FALSE,
         'args' => array(
           array(
             'name' => 'fn',
             'type' => 'string',
             'description' => 'Function to perform',
             'source' => array('path' => '0'),
             'optional' => TRUE,
             'default' => '0',
           ),
         ),
       ),
     ),
   ),
 );
  return $api;
}

function _mymod_test($fn) {
  return "Test is working";
}

http://myhost.dev/endpoint/test/custom returns just a 404

When i try it like this

function mymod_services_resources() {
 $api = array(
     'actions' => array(
       'custom' => array(
       ...

It is not shown in the Services list.

@Clive Yes, i tried to GET that resource. But when i try to POST

curl -i -X POST --header "X-CSRFToken: p2BMMItuM6QD9HvdPulxS5IBnXDBnczT46VoLLSqJLM" http://myhost.dev/endpoint/test/custom 

i get a HTTP/1.1 404 Not found: Could not find the controller.

But this solution - see the comments new user have to last 8 hours to answer own question :) yields to an other question.

How can i get the whole path, not just index 0 'source' => array('path' => '0'),

I like to get all path segments after the controller. in my case

myhost.dev/endpoint/test/foo/bar/1/2/3

i liked to get foo/bar/1/2/3 with index 0 i can get just foo.

  • 1
    IIRC actions can only be POST'd to, are you trying to GET that resource? – Clive Nov 5 '13 at 12:00
  • Hi Clive i tried to GET that resource. But POST is the same. i put the curl above. – surfi Nov 5 '13 at 12:17
2

I got it. I don't have to use myhost.dev/endpoint/test/retrieve in the first example. It is posible to use myhost.dev/endpoint/test/Whateveryouwant. So i understand, i can use the retrieve action and it is not mandatory part of the resource path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.