back

المعرفة

مركز المعرفة

كيفية تجاوز مورد REST من النواة الأساسية لـ Drupal

by editor | 18.10.2017

كيفية تجاوز مورد REST من النواة الأساسية لـ Drupal

أحيانًا تتطلب المشاريع منا العثور على حلول مبتكرة. مؤخرًا، احتجنا إلى تجاوز مورد REST الأساسي في Drupal 8، ونظرًا للتوثيق المحدود المتاح، قررنا مشاركة حلنا.

المبادئ الأساسية لتجاوز Drupal 8

يتعامل Drupal 8 مع التكوين بطريقة موحدة، مع تخزين المزيد من الميزات في النواة مقارنة بالإصدارات السابقة. هذا الخيار الهندسي يعني أن تجاوز الوظائف الأساسية يتطلب النظر بعناية لتجنب تلف النظام.

بينما يدعم Drupal 8 تجاوزات `$config` العالمية من خلال `Drupal\Core\Config\ConfigFactory::get()`, يتطلب تجاوز موارد REST نهجًا مختلفًا. يوفر النواة في Drupal 8 موارد REST أساسية للكيانات النظام، ولكن العمليات المعقدة غالبًا ما تتطلب توسيع هذه الفئات الأساسية.

دليل خطوة بخطوة لتجاوز مورد REST

1. إنشاء فئة جديدة

أولاً، قم بإنشاء فئة تمتد على فئة `EntityResource`:

namespace Drupal\my_module\Plugin\rest\resource;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Drupal\node\Entity\Node;
/**
* @RestResource(
*   id = "my_custom_node_rest_resource",
*   label = @Translation("Custom Node REST Resource"),
*   uri_paths = {
*     "canonical" = "/api/v1/node/{node}",
*     "create" = "/api/v1/node"
*   }
* )
*/
class MyCustomNodeResource extends EntityResource {
....
}

2. تنفيذ الأساليب المطلوبة

تجاوز الأساليب اللازمة من الفئة الأم:

/**
  * Responds to POST requests.
  *
  * @param array $data
  *   The request data.
  *
  * @return \Drupal\rest\ResourceResponse
  *   The response containing the node.
  */
 public function post($data) {
   // Your custom POST logic here
   $response = new ResourceResponse($result, 201);
   return $response;
 }
 /**
  * Responds to PATCH requests.
  *
  * @param \Drupal\Core\Entity\EntityInterface $entity
  *   The entity.
  * @param array $data
  *   The request data.
  *
  * @return \Drupal\rest\ResourceResponse
  *   The response.
  */
 public function patch($entity, array $data) {
   // Your custom PATCH logic here
   return new ResourceResponse($entity, 200);
 }

3. تكوين إعدادات مورد REST

أضف الآتي إلى تكوين وحدتك:

# config/install/rest.resource.my_custom_node_rest_resource.yml
langcode: en
status: true
dependencies:
 module:
   - my_module
   - serialization
id: my_custom_node_rest_resource
plugin_id: my_custom_node_rest_resource
granularity: resource
configuration:
 methods:
   - GET
   - POST
   - PATCH
 formats:
   - json
 authentication:
   - basic_auth

4. تنفيذ التحكم في الوصول

أضف التحقق من الوصول الصحيح:

/**
  * {@inheritdoc}
  */
 public function access($operation, array $args = []) {
   $entity = isset($args['entity']) ? $args['entity'] : NULL;
   
   switch ($operation) {
     case 'create':
       return $this->checkCreateAccess();
     case 'update':
       return $this->checkUpdateAccess($entity);
     default:
       return parent::access($operation, $args);
   }
 }
```

5. التعامل مع الاستثناءات

تنفيذ التعامل الصحيح مع الأخطاء:

 protected function handleException(\Exception $e) {
   watchdog_exception('my_module', $e);
   
   return new ResourceResponse([
     'error' => [
       'message' => $this->t('An error occurred while processing the request.'),
       'code' => 500,
     ],
   ], 500);
 }

أفضل الممارسات

  1. الحفاظ دائمًا على التوافق الخلفي عندما يكون ذلك ممكنًا
  2. توثيق نقاط النهاية المخصصة بشكل كامل
  3. تنفيذ التحكم الصحيح في الوصول
  4. إضافة التعامل مع الأخطاء بشكل شامل
  5. استخدام الحقن المعتمدة بدلاً من الدعوات الخدمية الثابتة
  6. إضافة التحقق الصحيح من صلاحية الطلب
  7. تضمين رؤوس الرد المناسبة

اختبار المورد المخصص الخاص بك

أضف اختبارات PHPUnit للتأكد من أن المورد الخاص بك يعمل كما هو متوقع:

namespace Drupal\Tests\my_module\Functional;
use Drupal\Tests\rest\Functional\ResourceTestBase;
class MyCustomNodeResourceTest extends ResourceTestBase {
 // Test implementation
}
Common Pitfalls to Avoid
  • عدم التعامل بشكل صحيح مع جميع طرق HTTP
  • نسيان تحديث تكوين REST بعد التغييرات
  • التعامل غير الكافي مع الأخطاء
  • عدم وجود تحكم في الوصول
  • عدم التحقق من صحة بيانات الإدخال
  • تنسيق الرد غير الصحيح

من خلال اتباع هذه الخطوات وأفضل الممارسات، يمكنك تجاوز موارد REST الأساسية في Drupal 8 بنجاح مع الحفاظ على استقرار النظام والأمان.

override-rest-drupal-core _0.jpg
  • المعرفة
    مركز المعرفة
    Fine-tuning LLaMA to Recreate Eminescu's Literary Style
    editor
  • المعرفة
    مركز المعرفة
    A New Era Begins: Drupal CMS 1.0 Launches
    editor
  • المعرفة
    مركز المعرفة
    Bringing AI to B2B
    editor