Task Classes
About 2 min
Overview
Task classes in Data Forge are designed to handle custom business logic that extends beyond standard CRUD operations. They allow developers to combine SQL queries, Entity manipulations, and other workflows to prepare or process data efficiently.
Key Features
1. Custom Business Logic
- Task methods represent specific actions or workflows.
- Developers can implement fully customized logic.
2. Integration
- Seamlessly call SQL methods and Entity methods within a Task class.
- Combine results into structured responses.
3. Error Handling
- Use
$this->setError()
to handle errors gracefully and provide meaningful error messages.
4. Reusability
- Modular methods are reusable across multiple endpoints and workflows.
5. Security
- Separate unauthenticated access logic with
GuestTask
methods for controlled data exposure.
Structure of a Task Class
Task classes extend the DataForge\Task
base class and are located in the App/DataForge/Task
directory.
Example Task Class
namespace App\DataForge\Task;
use DataForge\Task;
class Invoice extends Task
{
function detail($request)
{
$id = $request->get('invoiceId');
if (!$id) {
$this->setError('Invoice ID is missing!');
return false;
}
$invoice = \DataForge::getInvoice($id);
if (!$invoice) {
$this->setError(\DataForge::getError());
return false;
}
$response = [];
$response['Invoice'] = $invoice->toArray();
$response['Order'] = $invoice->Order->toArray();
$response['TotalDue'] = $invoice->TotalDue;
return $response;
}
}
Usage
Calling Task Methods via Endpoints
Task methods are accessed through the api/Task
or api/GuestTask
endpoints:
Example Request:
/api/Task/Invoice/detail?invoiceId=12345
GuestTask Methods
Overview
GuestTask methods allow public, unauthenticated access to specific data while ensuring sensitive information is not exposed.
- Method Naming: Methods must be prefixed with
guest
(e.g.,guestDetail
). - Security: Developers decide what data is exposed through controlled responses.
Example GuestTask Method
function guestdetail($request)
{
$id = $request->get('invoiceId');
if (!$id) {
$this->setError('Invoice ID is missing!');
return false;
}
$invoice = \DataForge::getInvoice($id);
if (!$invoice) {
$this->setError(\DataForge::getError());
return false;
}
$response = [];
$response['Id'] = $invoice->id;
$response['CustomerName'] = $invoice->Order->customer_name;
$response['Date'] = $invoice->invoice_date;
$response['TotalDue'] = $invoice->TotalDue;
return $response;
}
Example Request:
GET /api/guest-task/Invoice/detail?invoiceId=12345
Strengths of Task Classes
1. Centralized Workflow Management
- Task classes organize business logic into centralized, reusable modules.
2. Enhanced Security
- Segregation of authenticated and guest workflows ensures controlled data exposure.
3. Developer-Friendly
- Modular design simplifies maintenance and scalability.
- Easy-to-understand methods reduce onboarding time for new developers.
Best Practices
- Organized File Structure: Place task classes in
app/DataForge/Task
. - Error Handling: Use
setError()
to provide meaningful feedback. - Code Reusability: Modularize methods to avoid redundant logic.
- Seamless Integration: Leverage SQL and Entity classes within Task methods for powerful data preparation.
This improved version highlights key strengths, practical examples, and usage patterns of the Task class, providing clarity and scalability for your application logic.