Tab Management Service
Introduction
In the Platform module, you can easily add a custom service to tab settings. This feature allows you to implement specific logic for settings management using your own custom classes. To accomplish this, you need to use a standardized JSON structure that includes information about the service class and its input parameters. This guide explains the JSON structure and how to use custom services.
JSON Structure
The standard JSON structure for defining a custom service is as follows:
{
"setting": {
"customService": {
"className": "full class path of the service",
"params": {
"parameter_key1": "parameter_value1",
"parameter_key2": "parameter_value2"
}
}
}
}
setting: This key is the main section of your settings that defines the custom service as one of its keys.customService: The key indicating your custom service.className: The full path (namespace) of the class used as a custom service.params: Contains parameters that are sent to the custom service and will be processed in the service'shandlemethod.
Defining a Custom Service Class
Custom classes defined in JSON must follow a specific interface that ensures your class implements a handle method for data processing.
The CustomServiceInterface
Custom classes must implement this interface:
namespace App\Contracts;
interface CustomServiceInterface
{
/**
* The main method that the custom service must implement
*
* @param array $params
* @return array
*/
public function handle(array $params): array;
}
Implementing a Custom Class
A class defined as a custom service in JSON must implement the handle method, process the params inputs, and return an array. Here's an example:
namespace App\Services;
use App\Contracts\CustomServiceInterface;
class ExampleCustomService implements CustomServiceInterface
{
/**
* Process input parameters and return the result
*
* @param array $params
* @return array
*/
public function handle(array $params): array
{
// Process parameters
return [
'status' => 'success',
'processed_params' => $params,
];
}
}
Defining Services in Settings (JSON)
To define a custom service in settings, simply adhere to the JSON structure and correctly define the full class path and its input parameters.
Practical JSON Example
{
"setting": {
"customService": {
"className": "App\\Services\\ExampleCustomService",
"params": {
"param1": "value1",
"param2": "value2"
}
}
}
}
className: The full path to theExampleCustomServiceclass.params: Two input parameters,param1andparam2, that are sent to the service.
Implementing Settings in Tabs
Once you've defined your custom service in the JSON settings, the system automatically resolves this class and executes its handle method. The data returned from this method is then displayed in the settings tabs.
Important Notes
- Class-Interface Compatibility: Make sure your service class implements the
CustomServiceInterface. - Full Class Path: In the
classNamesection, specify the exact full class path with the correct namespace. - Custom Parameters: You can define any number of parameters in the
paramssection that will be processed in the custom service'shandlemethod. - In your custom view file, you can access the output of your custom class using the
$settingsvariable.
Conclusion
This feature allows you to add specific logic to settings tabs by defining custom services and their settings in JSON format, making them easy to manage. Custom services provide high flexibility for processing and displaying data, allowing you to add your desired logic to the system.
Use this capability to make your services dynamic and extensible!