drupal module Named Drupal Info Json with a json page to return base url
Creating a Drupal module named "Drupal Info Json" that provides a JSON page to return the base URL of your Drupal site involves several steps. Below, I'll detail how to create this module in Drupal 9 or later, including all necessary files and configurations.
Step 1: Create the Module Folder
Start by creating a new folder for your module in the custom modules directory of your Drupal installation:
/modules/custom/drupal_info_json
Step 2: Create the .info.yml File
Inside the drupal_info_json directory, create a file named drupal_info_json.info.yml. This file provides Drupal with metadata about your module.
name: 'Drupal Info Json'
type: module
description: 'Provides a JSON response with the site base URL.'
package: Custom
core_version_requirement: ^8 || ^9
Step 3: Create the .routing.yml File
Create a file named drupal_info_json.routing.yml in the same directory. This file will define the route to your JSON page.
drupal_info_json.response:
path: '/info-json'
defaults:
_controller: '\Drupal\drupal_info_json\Controller\InfoJsonController::baseUrlJson'
methods: [GET]
requirements:
_permission: 'access content'
This route is accessible at /info-json and will invoke the baseUrlJson method of the InfoJsonController class when visited. It allows only GET requests and requires the visitor to have the 'access content' permission.
Step 4: Create the Controller
Create a folder named src in the drupal_info_json directory, and then create another folder named Controller inside src.
Inside the Controller folder, create a file named InfoJsonController.php.
<?php
namespace Drupal\drupal_info_json\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Controller for outputting the base URL in JSON format.
*/
class InfoJsonController extends ControllerBase {
/**
* Returns the site base URL in JSON format.
*/
public function baseUrlJson() {
$base_url = \Drupal::request()->getSchemeAndHttpHost();
return new JsonResponse([ 'base_url' => $base_url, 'deep_base_url'=>$deep_base_url, ]);
}
}
This controller uses the JsonResponse class to return the base URL of the site. The base URL is retrieved using the getSchemeAndHttpHost method from the current request object, which automatically constructs the full base URL.
Step 5: Enable the Module
Once your module is ready, you need to enable it. You can do this via the Drupal admin interface under Extend, or use Drush:
drush en drupal_info_json
Step 6: Test Your Module
Visit http://yourdrupalsite.com/info-json in your web browser, or use a tool like curl:
curl http://yourdrupalsite.com/info-json
It should return the base URL of your Drupal site in JSON format, like:
{"base_url": "http://yourdrupalsite.com"}
Conclusion
This custom Drupal module, "Drupal Info Json," is now set up to provide a JSON endpoint that returns the base URL of the Drupal installation. This simple API endpoint can be a basis for more complex functionality, such as providing additional site information via JSON. Remember to clear Drupal's cache if you make changes to routes or controllers so that your changes are recognized.
留言
發佈留言