RESTful API untuk katalog course menggunakan:
- Laravel 12 (API Only)
- MySQL
- Laravel Sanctum (Authentication)
- Repository Pattern + Service Layer
- Global Response Format
- Authentication (Register & Login - Sanctum Token)
- Course Catalog (Public)
- Admin CRUD (Course, Topic, Language)
- Clean Architecture (Controller → Service → Repository)
- Global JSON Response Format
- Seeder (Course, Topic, Language, Admin)
- PHP 8.2+
- Laravel 12
- MySQL
- Laravel Sanctum
- Postman (API Testing)
git clone https://github.com/justsmith262/course-catalog-api.git
cd course-catalog-apicomposer installcp .env.example .envEdit file .env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=course_catalog
DB_USERNAME=root
DB_PASSWORD=Buat database baru di MySQL:
CREATE DATABASE course_catalog;php artisan key:generatephp artisan install:apiphp artisan migrate --seedSeeder akan membuat:
- 1 Admin User
- 3 Languages (Indonesia, English, Japan)
- 3 Topics
- 2 Courses
php artisan serveAPI Base URL:
http://127.0.0.1:8000/api
{
"email": "admin@mail.com",
"password": "123",
"role": "ADMIN"
}{
"code": 200,
"success": true,
"message": "Success",
"data": []
}{
"code": 404,
"success": false,
"message": "Error message",
"data": []
}Semua endpoint admin menggunakan:
Authorization: Bearer {token}
Buat Environment:
| Variable | Value |
|---|---|
| url | http://127.0.0.1:8000/api |
| token | (auto dari login) |
- AUTH
- COURSE (Public + Admin)
- TOPIC (Admin)
- LANGUAGE (Admin)
POST /register
{
"name": "User Test",
"email": "user@mail.com",
"password": "password"
}{
"code": 201,
"success": true,
"message": "Register success",
"data": {
"user": {},
"token": "..."
}
}POST /login
{
"email": "admin@mail.com",
"password": "123"
}Masukkan di tab Scripts:
const res = pm.response.json();
if (res.data && res.data.token) {
pm.environment.set("token", res.data.token);
console.log("Token saved:", res.data.token);
} else {
console.log("Token not found");
}GET /courses
Response:
{
"code": 200,
"success": true,
"message": "Courses fetched successfully",
"data": []
}GET /courses/{id}
POST /courses
Headers:
Authorization: Bearer {{token}}
Content-Type: application/json
Body:
{
"topic_id": 1,
"language_id": 2,
"title": "Laravel Clean Architecture",
"description": "Full REST API course",
"short_description": "Learn Laravel API",
"price": 199000,
"discount_rate": 10,
"thumbnail_url": "https://example.com/image.jpg",
"level": "BEGINNER"
}PUT /courses/{id}
DELETE /courses/{id}
GET /topics
POST /topics
Body:
{
"name": "Backend Development",
"description": "All backend topics",
"parent_id": null
}PUT /topics/{id}
DELETE /topics/{id}
GET /languages
POST /languages
Body:
{
"name": "English"
}PUT /languages/{id}
DELETE /languages/{id}
app/
├── Http/
│ ├── Controllers/Api
│ ├── Middleware
│ └── Requests
├── Services
├── Repositories
│ ├── Interfaces
│ └── Implementations
├── Models
Flow:
Request → Route → Controller → Service → Repository → Model → Resource → JSON Response
- auth:sanctum → Authentication
- admin → Role validation (ADMIN only)
- Login (get token)
- Token auto save ke environment
- Akses endpoint admin
- CRUD Course / Topic / Language
Token tidak dikirim atau invalid.
User bukan ADMIN.
Input request tidak sesuai rules.
Backend API built with Clean Architecture using Laravel 12.