name: create-endpoint description: Guides the creation of a new API endpoint in SimpleRest, including migrations, schemas, models, controllers, and ACL.
Creating a New API Endpoint
This skill guides you through the process of creating a complete API endpoint in the SimpleRest framework. It covers the standard locations as well as modules and packages.
Prerequisites
- Access to the terminal.
- Knowledge of the database connection to use.
- Defined table name and model name.
Step 1: Create Database Migration
General command:
php com make migration create_{table_name}_table
Context-Specific Migrations
If working within a Module:
php com make migrations:module {ModuleName} create_{table_name}_table --create
If working within a Package:
php com make migrations:package {package_name} create_{table_name}_table --create
Open the generated .php file and edit it.
Step 2: Execute Migration
Run all pending migrations:
php com migrate
Step 3: Generate Schema
Generate the schema file for the table:
php com make schema {table_name}
[!NOTE] If the table is in a different database connection, use
--from={connection_id}.
Step 4: Create Model
Generate the model for the table:
php com make model {ModelName}
[!TIP] This command uses the schema generated in the previous step. If you want to skip schema check, use
--no-check.
Step 5: Create API Controller
Generate the API controller:
php com make api {ControllerName}
[!IMPORTANT] The controller will be created in
app/Controllers/Api/by default. If you need it in a Module or Package, move it manually and adjust the namespace.
Step 6: Configure ACL (Access Control List)
- Open
config/acl.php. - Add the resource permissions for your table:
$acl->addResourcePermissions('{table_name}', ['show', 'list', 'create', 'update', 'delete'], '{role_name}');
Common roles: guest, registered, admin.
- Regenerate the ACL cache:
php com make acl --force
Step 7: Testing the Endpoint
Test the standard CRUD operations:
- List records:
GET /api/v1/{table_name} - Show record:
GET /api/v1/{table_name}/{id} - Create record:
POST /api/v1/{table_name} - Update record:
PUT /api/v1/{table_name}/{id} - Delete record:
DELETE /api/v1/{table_name}/{id}
Use curl, ApiClient utility or a tool like Playwright for UI-dependent tests.
Troubleshooting
- 404 Error: Ensure the controller exists, extends
MyApiController, and the table name matches. - Permission Error: Check
config/acl.phpand verify user roles. - Database Error: Ensure the schema file exists in
app/Schemas/and is correctly configured.