콘텐츠로 이동

프런트엔드 통합 모범 사례

XOOPS의 프런트엔드 통합 모범 사례

섹션 제목: “XOOPS의 프런트엔드 통합 모범 사례”

최신 XOOPS 모듈에는 반응형 디자인, 적절한 JavaScript 패턴 및 AJAX 기능을 갖춘 깔끔한 프런트엔드 통합이 필요합니다.

{* Include Bootstrap CSS *}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"
rel="stylesheet">
{* Your content *}
<div class="container mt-4">
<h1>My Module</h1>
</div>
{* Include Bootstrap JS *}
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<div class="card">
<div class="card-body">
<form method="POST" class="needs-validation">
<div class="mb-3">
<label for="username" class="form-label">Username</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
<div class="table-responsive">
<table class="table table-striped">
<thead class="table-dark">
<tr>
<th>ID</th>
<th>Username</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{foreach from=$users item=user}
<tr>
<td>{$user.id|escape}</td>
<td>{$user.username|escape}</td>
<td>{$user.email|escape}</td>
<td>
<a href="?op=edit&id={$user.id}" class="btn btn-sm btn-warning">Edit</a>
<a href="?op=delete&id={$user.id}" class="btn btn-sm btn-danger">Delete</a>
</td>
</tr>
{/foreach}
</tbody>
</table>
</div>
// Module pattern
const MyModule = {
init: function() {
this.initTooltips();
this.initEventListeners();
},
initTooltips: function() {
// Initialize Bootstrap tooltips
},
initEventListeners: function() {
// Attach event handlers
document.addEventListener('click', (e) => {
if (e.target.classList.contains('btn-delete')) {
return confirm('Delete this item?');
}
});
},
notify: function(message, type = 'info') {
const alertClass = `alert-${type}`;
const html = `
<div class="alert ${alertClass} alert-dismissible fade show">
${message}
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
`;
document.body.insertAdjacentHTML('afterbegin', html);
}
};
// Initialize when DOM ready
document.addEventListener('DOMContentLoaded', () => MyModule.init());
const AjaxHelper = {
request: function(url, options = {}) {
const defaults = {
method: 'GET',
headers: { 'Content-Type': 'application/json' }
};
const config = { ...defaults, ...options };
return fetch(url, {
method: config.method,
headers: config.headers,
body: config.body ? JSON.stringify(config.body) : null
})
.then(response => response.json())
.catch(error => console.error('Error:', error));
},
get: function(url) {
return this.request(url, { method: 'GET' });
},
post: function(url, data) {
return this.request(url, { method: 'POST', body: data });
}
};
// Usage
AjaxHelper.get('/modules/mymodule/api/users')
.then(response => {
if (response.success) {
MyModule.notify('Loaded successfully', 'success');
}
});
<?php
class ApiController
{
public function getUsersAction()
{
try {
$users = $this->userService->getActiveUsers();
return [
'success' => true,
'data' => array_map(fn($u) => $u->toArray(), $users),
];
} catch (\Exception $e) {
http_response_code(500);
return ['success' => false, 'message' => $e->getMessage()];
}
}
public function createUserAction()
{
try {
$input = json_decode(file_get_contents('php://input'), true);
$userDTO = $this->userService->register(
$input['username'],
$input['email'],
$input['password']
);
http_response_code(201);
return ['success' => true, 'data' => $userDTO->toArray()];
} catch (\Exception $e) {
http_response_code(400);
return ['success' => false, 'message' => $e->getMessage()];
}
}
}
?>
  • 일관된 반응형 디자인을 위해 부트스트랩을 사용합니다.
  • jQuery가 아닌 최신 JavaScript(ES6+)를 사용하세요.
  • XMLHttpRequest 대신 Fetch API를 사용하세요.
  • 항상 서버측 유효성을 검사합니다.
  • AJAX 요청에 로딩 상태 표시
  • 명확한 오류 메시지 제공
  • 의미론적 HTML을 사용하라
  • 이미지 및 자산 최적화
  • 모든 요청에 HTTPS를 사용합니다.

참조:

  • 자산 정리를 위한 코드 정리
  • 오류 표시를 위한 오류 처리
  • 컨트롤러 통합을 위한../Patterns/MVC-Pattern

태그: #모범 사례 #프론트엔드 #부트스트랩 #javascript #ajax #모듈 개발