Guidelines for developing HelpDesk API.
# Clone and setup
git clone https://github.com/yourusername/helpdesk-api.git
cd helpdesk-api
composer install
# Configure
cp .env.example .env
# Edit .env with your database
# Initialize
php spark migrate
php spark db:seed DatabaseSeeder
# Start development
php spark serveapp/
├── Controllers/ REST API controllers
├── Models/ Database models with validation
├── Services/ Business logic layer
├── Config/ Configuration
└── Database/
└── Seeds/ Database seeders
tests/
└── Unit/ Service tests and integration tests
docs/ Documentation files
public/
└── openapi.json OpenAPI specification
vendor/ Composer dependencies
Follow PSR-12:
- 4 spaces for indentation
- Meaningful names for variables/functions
- Type hints on all parameters and return types
- Single responsibility principle
public function createTicket(array $data): array
{
if (!$this->model->validate($data)) {
return [
'success' => false,
'errors' => $this->model->errors()
];
}
$ticketId = $this->model->insert($data);
return [
'success' => true,
'data' => $this->model->find($ticketId)
];
}No emojis or symbols. Use clear language:
// Good
// Calculate total cost including tax
$total = $price * 1.20;
// Bad
// Calculate total cost including tax
$total = $price * 1.20; // adds 20% taxAll new features require tests.
tests/Unit/Services/YourServiceTest.php
public function testCreateTicket()
{
// Arrange
$data = [
'title' => 'Test Ticket',
'category_id' => 1
];
// Act
$result = $this->service->createTicket($data);
// Assert
$this->assertTrue($result['success']);
$this->assertNotEmpty($result['data']);
}# All tests
php spark test
# Specific file
vendor/bin/phpunit tests/Unit/Services/TicketServiceTest.php
# Specific test
vendor/bin/phpunit --filter testCreateTicket// app/Models/YourModel.php
class YourModel extends Model
{
protected $table = 'your_table';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $allowedFields = ['field1', 'field2'];
protected $validationRules = [
'field1' => 'required|string|max_length[100]'
];
}// app/Services/YourService.php
class YourService
{
protected $model;
public function __construct()
{
$this->model = new YourModel();
}
public function create(array $data): array
{
// Validate
if (!$this->model->validate($data)) {
return ['success' => false, 'errors' => $this->model->errors()];
}
// Insert
$id = $this->model->insert($data);
return ['success' => true, 'data' => $this->model->find($id)];
}
}// app/Controllers/YourController.php
class YourController extends ResourceController
{
protected $service;
protected $format = 'json';
public function __construct()
{
$this->service = new YourService();
}
public function index()
{
$result = $this->service->getAll();
return $this->respond($result);
}
public function create()
{
$data = $this->request->getJSON(true);
$result = $this->service->create($data);
if ($result['success']) {
return $this->respondCreated($result['data']);
}
return $this->respond($result, 400);
}
}// app/Config/Routes.php
$routes->group('api/v1', static function ($routes) {
$routes->resource('your-resource');
});Add to public/openapi.json in the paths and components sections.
// tests/Unit/Services/YourServiceTest.php
class YourServiceTest extends CIUnitTestCase
{
protected $service;
protected function setUp(): void
{
parent::setUp();
$this->service = new YourService();
}
public function testCreate()
{
$data = ['field1' => 'value1'];
$result = $this->service->create($data);
$this->assertTrue($result['success']);
$this->assertNotEmpty($result['data']['id']);
}
}For schema changes:
// Create migration
php spark make:migration CreateNewTable
// Edit app/Database/Migrations/
// Then run
php spark migrateEdit .env:
CI_ENVIRONMENT = developmentlog_message('error', 'Error message here');
log_message('info', 'Info message here');View logs in writable/logs/.
- Use indexes on frequently queried columns
- Lazy load relationships when needed
- Cache frequently accessed data
- Use pagination for large result sets
- Avoid N+1 query problems
Add new feature
Fix bug in service
Update documentation
Refactor controller logic
feature/ticket-assignment- New featuresbugfix/null-pointer- Bug fixesdocs/readme-update- Documentationrefactor/service-cleanup- Refactoring
For production deployment:
- Set
CI_ENVIRONMENT = productionin .env - Configure proper database backups
- Set up HTTPS/TLS
- Enable logging and monitoring
- Configure rate limiting (planned)
- Implement authentication (planned)
For development questions or contributions, reach out to Zoran Makrevski at zoran@makrevski.com