Làm cách nào để viết mô-đun dịch vụ cho Drupal 7 và Dịch vụ 3.x?


11

Ai đó có thể vui lòng cho hướng dẫn cách viết mô-đun dịch vụ đơn giản cho Dịch vụ 3.x và Drupal 7.x không? Tôi chỉ đơn giản là không thể tìm thấy bất kỳ hoạt động với D7. Bất cứ điều gì! Chỉ cần một phiên bản làm lại của mô-đun echo_service sẽ làm!

Ít nhất xin vui lòng liên kết đến một ví dụ làm việc. Cảm ơn.



Các liên kết ở trên là một ví dụ phong nha cho bất cứ ai tìm kiếm.
dùng968416

Bài viết này của IBM đã giúp tôi rất nhiều với phần mềm ibm.com/developerworks/opensource/l
Library

Câu trả lời:


5

Ngoài liên kết được đề cập, đây là một số mã từ mô-đun tôi đã viết sử dụng dịch vụ web. Trong "cấu trúc", dịch vụ sẽ được bật và điểm cuối được xác định. Bạn cũng đặt các loại trả lại được phép ở đó.

Vì vậy, nếu bạn xác định 'api' là điểm cuối và muốn một mảng json, bạn sẽ thực hiện một yêu cầu như yoursite.com/api/servicename/arg1/arg2.json. Bạn xác định tên dịch vụ trong hook_service_resource.

<?php

// $Id$
/* * **********************************************************************************************
 * @file
 * Based on the RESTful API shell module http://drupal.org/node/1034540
 */

/* * *************************************************************************************************
 * Include necessary files
 */
require_once (drupal_get_path('module', 'graph_data_api') . '/model/highchart_graph.php');

/* * *************************************************************************************************
 * Implementation of hook_help().
 * @see http://api.drupal.org/api/function/hook_help/6
 */

function graph_data_api_help($path, $arg) {
  $msg = t('<p>Provides a API for graph data for use with Highcharts.</p>');
  switch ($path) {
    case 'admin/help#graph_data_api':
      return $msg;
    case 'admin/modules#description':
      return $msg;
  }
}

/* * *************************************************************************************************
 * Implementation of hook_disable()
 * @see http://api.drupal.org/api/function/hook_disable/6
 */

function graph_data_api_disable() {
  cache_clear_all('services:methods', 'cache');
  //eco_debug( '*** graph_data_api_disable() called!' );
}

/* * *************************************************************************************************
 * Implementation of hook_enable()
 * @see http://api.drupal.org/api/function/hook_enable/6
 */

function graph_data_api_enable() {
  cache_clear_all('services:methods', 'cache');
  //eco_debug( '*** graph_data_api_enable() called!' );
}

/* * *************************************************************************************************
 * Implementation of hook_perm().
 * @see http://api.drupal.org/api/function/hook_perm/6
 */

function graph_data_api_permission() {
  return array(
      'request graph data' => array(
          'title' => t('Request graph data'),
          'description' => t('Allows user to use the graph api for Highchart graphs'),
      ),
  );
}

/* * *************************************************************************************************
 * Access callback 
 * For now only view/retrieve is implemented and if the user is logged in, he gets access
 */

function _graph_data_api_graphdata_access($op) {
  global $user;
  $access = FALSE;

  switch ($op) {
    case 'view':
      if ($user->uid) {
        $access = TRUE;
      }
      break;
  }

  return $access;
}

/* * *************************************************************************************************
 * Implementation of hook_services_resources().
 * For now only retrieve with a GET request is implemented
 */

function graph_data_api_services_resources() {
  return array(
      'graphdata' => array(
          'retrieve' => array('help' => 'Retrieves graphdata',
              'callback' => '_graph_data_api_graphdata_retrieve',
              'access callback' => '_graph_data_api_graphdata_access',
              'access arguments' => array('view'),
              'access arguments append' => FALSE,
              'args' => array(
                  array('name' => 'parameters',
                      'type' => 'string',
                      'description' => 'The parameters that define requested data',
                      'source' => array('path' => '0'), // first argument in the url 
                      'optional' => FALSE,
                  ),
              ),
          ),
      ),
  );
}

/* * *************************************************************************************************
 * Callback for the retrieve resource
 */

function _graph_data_api_graphdata_retrieve($arg) {

  $data = 'hello world';
  return $data;
}

Đáng nói là các tài nguyên đều được lưu trong bộ nhớ cache và bạn cần xóa thủ công tất cả các bộ nhớ cache (admin / config /
Development
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.