2015-11-03 55 views
3

我在我的智慧的结尾试图破译为什么这是行不通的。我试图从我的Drupal 7实例设置一个REST服务器。目前专注于不需要身份验证(一旦设法让它谈话,我会设置它)。Drupal 7 - 无法获得服务终端显示

下面是相关的代码位:

mymodule.module

/** 
* Implements hook_ctools_plugin_api(). 
* Declares the mymodule services endpoint (stored in 
* mymodule.services.inc). Note that the referenced ctools 
* hook obviates creating this endpoint through the UI. 
*/ 
function mymodule_ctools_plugin_api($owner, $api) { 
    if ($owner == 'services' && $api == 'services') { 
    return array(
     'version' => 3, 
     'file' => 'mymodule.services.inc', 
     'path' => drupal_get_path('module', 'mymodule'), 
    ); 
    } 
} 
/** 
* Implements hook_services_resources 
* Defines the resources available via services module (REST, in this case). 
* 
* @return array The definition array 
*/ 
function mymodule_services_resources() { 
    watchdog('mymodule', __FUNCTION__, array(), WATCHDOG_NOTICE, 'link'); 
    return array(
     '#api_version' => 3001, 
     'test' => array(
     'retrieve' => array(
      'help' => t("A test of the REST api"), 
      'file' => array(
      'type' => 'inc', 
      'module' => 'mymodule', 
      'name' => 'resources/test_resource', 
     ), 
      'access arguments'=>array('access content'), 
      'callback' => 'mymodule_services_test', 
      'args' => array(
      array(
       'name' => 'id', 
       'type' => 'int', 
       'description' => t("a test of rest arguments"), 
       'source' => array('path' => '0'), 
       'optional' => FALSE, 
      ), 
     ), 
     ), 
     'index' => array(
      'help' => t('A test of the REST api index functionality'), 
      'file' => array(
      'type' => 'inc', 
      'module' => 'mymodule', 
      'name' => 'resources/test_resource', 
     ), 
      'callback' => 'mymodule_services_test', 
     ), 

    ), 
    ); 
} 

资源/ test_resources.inc

/** 
* 
*/ 
function mymodule_services_test() { 
    watchdog('mymodule', __FUNCTION__, array(), WATCHDOG_NOTICE, 'link'); 
    $result = array('foo' => 'bar'); 
    drupal_json_output($result); 
} 
/** 
* Access callback for test services (currently unused) 
* @param string $op The operation being performed: creat 
* @param [type] $args [description] 
* @return [type]  [description] 
*/ 
function mymodule_services_test_access($op, $args) { 
    watchdog('mymodule', __FUNCTION__, array(), WATCHDOG_NOTICE, 'link'); 
    return TRUE; 
} 

mymodule.services.inc

/** 
* @file 
*/ 

/** 
* Implements hook_default_services_endpoint(). 
*/ 
function mymodule_default_services_endpoint() { 
    watchdog('mymodule', __FUNCTION__, array(), WATCHDOG_NOTICE, 'link'); 
    $endpoint = new stdClass(); 
    $endpoint->disabled = FALSE; //Edit this to true to make a default endpoint disabled initially 
    $endpoint->api_version = 3; 
    $endpoint->name = 'mymodule_rest_api_v1'; 
    $endpoint->server = 'rest_server'; 
    $endpoint->path = 'api/mymodule/v1'; 
    $endpoint->authentication = array(); 
    $endpoint->server_settings = array(
    'formatters' => array(
     'json' => TRUE, 
     'bencode' => FALSE, 
     'jsonp' => FALSE, 
     'php' => FALSE, 
     'xml' => FALSE, 
    ), 
    'parsers' => array(
     'application/json' => TRUE, 
     'text/xml' => TRUE, 
     'application/vnd.php.serialized' => FALSE, 
     'application/x-www-form-urlencoded' => FALSE, 
     'application/xml' => FALSE, 
     'multipart/form-data' => FALSE, 
    ), 
); 
    $endpoint->resources = array(); 
    $endpoint->debug = 0; 
    return array('mymodule'=>$endpoint); 
} 

我删除了该服务的用户界面,内置的定义,可以看到,它已成功地从码重建,再加上,当我访问“API/mymodule中/ V1 /”,我看到消息“服务端点“mymodule_rest_api_v1”已成功安装。',所以我知道hook_default_services_endpoint和hook_ctools_plugin_api工作正常。

不管出于何种原因,我无法获得mymodule_services_resources中定义的任何路径来识别为有效。我已经删除了所有的访问限制,多次清除缓存 - 总是,当我点击以'test'结尾的任何网址时(例如,https://[my-domain]/api/mymodule/v1/testhttps://[my-domain]/api/mymodule/v1/test/1都产生了“未找到:无法找到资源测试。”)。

任何意见,将不胜感激。

回答

2

想通了。发回原因以防其他人遇到此问题。

hook_services_resources声明模块提供的资源。它不做的是启用这些资源。回到我的服务端点的资源标签下,我发现“测试”可用作可以公开的新资源。更重要的是,它没有启用。

启用它并将其导出到代码后,我能mymodule_default_services_endpoint的内容改成这样:

function mymodule_default_services_endpoint() { 
    watchdog('mymodule', __FUNCTION__, array(), WATCHDOG_NOTICE, 'link'); 
    $endpoint = new stdClass(); 
    $endpoint->disabled = FALSE; /* Edit this to true to make a default endpoint disabled initially */ 
    $endpoint->api_version = 3; 
    $endpoint->name = 'mymodule_rest_api_v1'; 
    $endpoint->server = 'rest_server'; 
    $endpoint->path = 'api/mymodule/v1'; 
    $endpoint->authentication = array(
    'services' => 'services', 
); 
    $endpoint->server_settings = array(
    'formatters' => array(
     'json' => TRUE, 
     'bencode' => FALSE, 
     'jsonp' => FALSE, 
     'php' => FALSE, 
     'xml' => FALSE, 
    ), 
    'parsers' => array(
     'application/json' => TRUE, 
     'text/xml' => TRUE, 
     'application/vnd.php.serialized' => FALSE, 
     'application/x-www-form-urlencoded' => FALSE, 
     'application/xml' => FALSE, 
     'multipart/form-data' => FALSE, 
    ), 
); 
    $endpoint->resources = array(
    'test' => array(
     'operations' => array(
     'retrieve' => array(
      'enabled' => '1', 
     ), 
     'index' => array(
      'enabled' => '1', 
     ), 
    ), 
    ), 
); 
    $endpoint->debug = 0; 
    return array('mymodule' => $endpoint); 
} 

现在我得到验证错误,这是我希望能对付。希望这可以帮助某人。