DELETE Operation Cheatsheet (Admin Panel)
This cheatsheet provides step-by-step instructions for implementing the DELETE operation in your Admin Panel.
What is the DELETE Operation in Admin Panel?
Section titled “What is the DELETE Operation in Admin Panel?”The DELETE operation allows administrators to remove items (products, categories, etc.) from your database. It involves:
- User clicks delete link/button → Browser sends GET request to delete route
- Handle deletion → Remove the item from database and redirect back to list
Admin Panel Structure:
- Routes use
/adminprefix (e.g.,/admin/products/{id}/delete). - Typically accessed via delete button/link on list or detail pages.
- Authentication middleware will be added later to protect these routes.
Prerequisites
Section titled “Prerequisites”Before implementing DELETE operations, ensure you have:
- ✅ ProductsModel created (extends
BaseModel, injectsPDOService) - ✅ ProductsController created (extends
BaseController, injectsContainerandProductsModel) - ✅ List view exists (
app/Views/admin/products/products.IndexView.php) - Where you’ll add delete links
Step 1: Register Admin Route
Section titled “Step 1: Register Admin Route”In app/Routes/web-routes.php, register one route inside the /admin route group:
Instructions:
Section titled “Instructions:”- DELETE route (GET): Inside the admin group, register a GET route for
/products/{id}/deletethat calls thedelete()method- The full URI will be:
/admin/products/{id}/delete - Set a named route:
products.delete
- The full URI will be:
Example structure:
$app->group('/admin', function ($group) { // TODO: Add GET /products/{id}/delete route here});// Note: Later you'll add ->add(AdminAuthMiddleware::class) to protect these routesStep 2: Implement Controller Callback Method
Section titled “Step 2: Implement Controller Callback Method”In app/Controllers/ProductsController.php, implement the delete method:
Method: delete() - Handle Deletion
Section titled “Method: delete() - Handle Deletion”What this method should do (see hints below):
- Extract the product ID from the route parameters.
- Validate that the ID is valid (not empty, is a number).
- Call the model’s
delete()method to remove the item from database. - Redirect back to the product list page.
Method signature:
public function delete(Request $request, Response $response, array $args): Response{ // TODO: 1. Get the product ID from $args['id']
// TODO: 2. Validate the ID (optional but recommended)
// TODO: 3. Delete the product using $this->model->delete($id)
// TODO: 4. Redirect to 'products.index' (the product list)}Hints:
- Use
(int) $args['id']to get the ID from route parameters. - Use
$this->model->delete($id)to delete the product. - Use
$this->redirect()to redirect to the product list. - No form data needed - the ID comes from the URL.
Step 3: Implement Model Method
Section titled “Step 3: Implement Model Method”In app/Models/ProductsModel.php, create a method to delete the record:
Method: delete() - Remove Record from Database
Section titled “Method: delete() - Remove Record from Database”What this method should do (see hints below):
- Execute a DELETE query to remove the record with the given ID.
- Return the number of affected rows (should be 1 if successful).
Method signature:
public function delete(int $id): int{ // TODO: Execute DELETE query using $this->execute() // - Use named parameter :id for security // - Return the number of affected rows}Hints:
- Use
$this->execute()for DELETE queries. - Use SQL:
DELETE FROM products WHERE id = :id. - Use named parameter:
['id' => $id]. - Return value will be the number of deleted rows (0 or 1).
Step 4: Add Delete Links to List View
Section titled “Step 4: Add Delete Links to List View”In app/Views/admin/products/products.IndexView.php, add delete links for each product in the table:
What to add:
Section titled “What to add:”- In your table’s
<tr>loop, add a<td>element for the delete link - Inside the
<td>, create an anchor tag with:hrefattribute pointing to:<?=APP_ADMIN_BASE_URL?>/products/{id}/deleteonclickattribute with JavaScript confirmation:return confirm('Are you sure you want to delete this product?')- Link text: “Delete”
Hints:
- Replace
{id}with the actual product ID using PHP:<?php echo $product['id']; ?>. - Add
onclick="return confirm('...')"for JavaScript confirmation dialog. - Style the delete link with red/danger color to indicate destructive action.
- If you have an Edit link, add the Delete link in the same
<td>with a separator (e.g.,|or space).
Complete Workflow (Admin Panel)
Section titled “Complete Workflow (Admin Panel)”When deleting a product, here’s what happens:
1. User clicks "Delete" link → Browser requests GET /admin/products/{id}/delete ↓2. Router calls ProductsController::delete() ↓3. Controller: - Extracts product ID from route parameters - Validates the ID - Calls model's delete() method ↓4. Model: - Executes DELETE query - Returns number of affected rows ↓5. Controller: Redirects to GET /admin/products (product list) ↓6. User sees the updated product list (without the deleted item)Validation & Error Handling
Section titled “Validation & Error Handling”Your delete() method should validate:
Required validations:
- ✅ Product ID must not be empty
- ✅ Product ID must be a valid integer
- ✅ Product ID must be greater than 0
Validation hints:
- Use
if ($id <= 0)to check for invalid IDs. - Use
try-catchblocks for database errors.
Common Mistakes to Avoid
Section titled “Common Mistakes to Avoid”- Wrong route pattern → Use
/products/{id}/delete, not/products/delete/{id} - Forgetting confirmation → Always add JavaScript confirmation to prevent accidental deletions
- Hardcoded IDs → Use
$args['id']from route parameters, don’t hardcode IDs - Not validating ID → Always validate the ID before attempting to delete
- No error handling → Use try-catch blocks for database errors
- Rendering after delete → Always redirect, never render directly after deletion
- Forgetting admin prefix → Delete route must be inside
/admingroup
Testing Checklist
Section titled “Testing Checklist”After implementation, test these scenarios:
Happy Path (Success)
Section titled “Happy Path (Success)”- ✅ Navigate to
/admin/products→ Delete links appear for each product - ✅ Click delete link → Confirmation dialog appears
- ✅ Click “OK” in dialog → Deletion happens and redirects to product list
- ✅ Check database → Product is removed
Error Handling
Section titled “Error Handling”- ✅ Try to delete non-existent ID → Handles gracefully
- ✅ Click “Cancel” in confirmation dialog → No deletion occurs
Edge Cases
Section titled “Edge Cases”- ✅ Delete product and refresh page → No duplicate deletion (PRG pattern working)