อะไรคือฟังก์ชั่นที่เทียบเท่าที่ฉันควรใช้สำหรับการจัดการ HTTP?


17

ดูหน้าที่ที่ระบุไว้ในหน้าการจัดการ HTTP สำหรับ Drupal 7ฉันสังเกตเห็นว่าฟังก์ชั่นต่อไปนี้ไม่มีอยู่ใน Drupal 8 อีกต่อไป (ลิงก์สำหรับหน้าเอกสารคู่มือ Drupal 7 ซึ่งลิงก์ไปยังเอกสารประกอบ Drupal 8 สำหรับสิ่งเหล่านั้น ฟังก์ชันขาดหายไป)

ฉันควรใช้ฟังก์ชั่น / วิธีการใดใน Drupal 8 แทน


1
คำถามนี้เป็นส่วนหนึ่งของคำถามเกี่ยวกับความแตกต่างระหว่าง Drupal 7 และ Drupal 8
kiamlaluno

คำตอบ:


16

นี่คือฟังก์ชัน / วิธี / คลาสที่ควรใช้ในรหัส Drupal 8.6.x

  • drupal_access_denied()ถูกแทนที่จากคลาสAccessDeniedHttpException การเรียกกลับหน้าเว็บที่ต้องการส่งคืนข้อผิดพลาดการปฏิเสธการเข้าถึงควรใช้รหัสที่คล้ายกับรหัสต่อไปนี้

    // system_batch_page()
    public function batchPage(Request $request) {
      require_once $this->root . '/core/includes/batch.inc';
      $output = _batch_page($request);
      if ($output === FALSE) {
        throw new AccessDeniedHttpException();
      }
      elseif ($output instanceof Response) {
        return $output;
      }
      elseif (isset($output)) {
        $title = isset($output['#title']) ? $output['#title'] : NULL;
        $page = [
          '#type' => 'page',
          '#title' => $title,
          '#show_messages' => FALSE,
          'content' => $output,
        ];
    
        // Also inject title as a page header (if available).
        if ($title) {
          $page['header'] = [
            '#type' => 'page_title',
            '#title' => $title,
          ];
        }
        return $page;
      }
    }
  • แทนที่จะdrupal_get_query_array()มีparse_query()(ฟังก์ชั่นในGuzzleHttp\Psr7namespace) ซึ่งเป็นส่วนหนึ่งของ Guzzle

  • drupal_goto()ถูกแทนที่จากRedirectResponseชั้นเรียน การเรียกกลับหน้าเว็บที่ต้องเปลี่ยนเส้นทางผู้ใช้ควรใช้รหัสที่คล้ายกับรหัสต่อไปนี้ (ขอให้สังเกตว่าตัวจัดการการส่งแบบฟอร์มไม่ควรใช้คลาสนี้)

    // AddSectionController::build()
    public function build(SectionStorageInterface $section_storage, $delta, $plugin_id) {
      $section_storage
        ->insertSection($delta, new Section($plugin_id));
      $this->layoutTempstoreRepository
        ->set($section_storage);
      if ($this->isAjax()) {
        return $this->rebuildAndClose($section_storage);
      }
      else {
        $url = $section_storage->getLayoutBuilderUrl();
        return new RedirectResponse($url->setAbsolute()->toString());
      }
    }
  • drupal_http_request()ถูกแทนที่จากบริการ Drupal 8 ที่ใช้ส่วนต่อประสานกับClientInterface รหัส Drupal 8 ควรคล้ายกับรหัสต่อไปนี้

    // system_retrieve_file()
    try {
      $data = (string) \Drupal::httpClient()->get($url)->getBody();
      $local = $managed ? file_save_data($data, $path, $replace) : file_unmanaged_save_data($data, $path, $replace);
    } catch (RequestException $exception) {
      \Drupal::messenger()->addError(t('Failed to fetch file due to error "%error"', ['%error' => $exception->getMessage()]));
      return FALSE;
    }
  • drupal_not_found()ถูกแทนที่จากคลาสNotFoundHttpException การเรียกกลับหน้าควรใช้รหัสที่คล้ายกับรหัสต่อไปนี้

    // BookController::bookExport()
    public function bookExport($type, NodeInterface $node) {
      $method = 'bookExport' . Container::camelize($type);
    
      // @todo Convert the custom export functionality to serializer.
      if (!method_exists($this->bookExport, $method)) {
        $this->messenger()->addStatus(t('Unknown export format.'));
        throw new NotFoundHttpException();
      }
      $exported_book = $this->bookExport->{$method}($node);
      return new Response($this->renderer->renderRoot($exported_book));
    }
  • drupal_site_offline() ควรถูกแทนที่โดยสมาชิกเหตุการณ์คล้ายกับสมาชิกต่อไปนี้

    public static function getSubscribedEvents() {
      $events[KernelEvents::REQUEST][] = ['onKernelRequestMaintenance', 30];
      $events[KernelEvents::EXCEPTION][] = ['onKernelRequestMaintenance'];
      return $events;
    }
    
    public function onKernelRequestMaintenance(GetResponseEvent $event) {
      $request = $event->getRequest();
      $route_match = RouteMatch::createFromRequest($request);
      if ($this->maintenanceMode->applies($route_match)) {
        // Don't cache maintenance mode pages.
        \Drupal::service('page_cache_kill_switch')->trigger();
        if (!$this->maintenanceMode->exempt($this->account)) {
          // Deliver the 503 page if the site is in maintenance mode and the
          // logged in user is not allowed to bypass it.
          // If the request format is not 'html' then show default maintenance
          // mode page else show a text/plain page with maintenance message.
          if ($request->getRequestFormat() !== 'html') {
            $response = new Response($this->getSiteMaintenanceMessage(), %03, ['Content-Type' => 'text/plain']);
            $event->setResponse($response);
            return;
          }
          drupal_maintenance_theme();
          $response = $this->bareHtmlPageRenderer->renderBarePage([          '#markup' => $this->getSiteMaintenanceMessage()], $this->t('Site under maintenance'), 'maintenance_page');
          $response->setStatusCode(503);
          $event->setResponse($response);
        }
        else {
          // Display a message if the logged in user has access to the site in
          // maintenance mode. However, suppress it on the maintenance mode
          // settings page.
          if ($route_match->getRouteName() != 'system.site_maintenance_mode') {
            if ($this->account->hasPermission('administer site configuration')) {
              $this->messenger->addMessage($this
          ->t('Operating in maintenance mode. <a href=":url">Go online.</a>', [':url' => $this->urlGenerator->generate('system.site_maintenance_mode')]), 'status', FALSE);
            }
            else {
              $this->messenger->addMessage($this->t('Operating in maintenance mode.'), 'status', FALSE);
            }
          }
        }
      }
    }
    • drupal_encode_path() ถูกแทนที่ด้วย UrlHelper::encodePath()
    • drupal_get_query_parameters() ถูกแทนที่ด้วย UrlHelper::filterQueryParameters()
    • drupal_http_build_query()ถูกแทนที่ด้วยUrlHelper::buildQuery()ซึ่งจะถูกลบออกเมื่อ Drupal core ต้องการอย่างน้อย PHP 5.4 (ณ จุดนั้นจะสามารถใช้งานได้โดยตรง http_build_query())
    • drupal_parse_url() ถูกแทนที่ด้วย UrlHelper::parse()

โปรดสังเกตว่าเมื่อเทียบกับ Drupal รุ่นก่อนมีการเปลี่ยนแปลงที่สำคัญบางประการ ตัวอย่างเช่นวิธีการบางอย่างที่อยู่ในUrlชั้นเรียนได้รับการย้ายในUrlHelperชั้นเรียน; บางคลาส Guzzle ไม่ได้ใช้อีกต่อไป


ลิงก์ API บางส่วนอาจไม่ทำงาน
rudolfbyker

มันเป็นไปได้ที่ฟังก์ชั่นเหล่านั้นจะถูกลบออกจาก Drupal core ฉันจะตรวจสอบว่าลิงก์ใดที่ตายแล้วและลบออก
kiamlaluno

ดูเหมือนว่าลิงก์บางรายการจะใช้ไม่ได้อีกต่อไป แต่คลาส / ฟังก์ชัน / วิธียังคงมีอยู่ พวกเขาเพียงแค่เปลี่ยนรูปแบบลิงก์หรือคลาส / ฟังก์ชัน / วิธีถูกย้ายไปยังไฟล์อื่น
kiamlaluno
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.