CRUD Operations in Slim Framework
What is CRUD?
Section titled “What is CRUD?”CRUD is an acronym for Create, Read, Update, and Delete. It is a set of operations that are used to manage data in a database.
This guide walks you through creating a new resource with complete CRUD operations in your Slim MVC application.
Prerequisites
Section titled “Prerequisites”- Familiar with MVC pattern and the provided Slim Skeleton Project ↗-
- Understand the BaseController methods (render()andredirect())
- Understand the BaseModel methods
- Familiar with PSR-7 Request/Response objects
Step 1: Create the Controller
Section titled “Step 1: Create the Controller”- Create a new controller class in app/Controllers/(if it doesn’t exist)
- Name it using PascalCase with “Controller” suffix (e.g., ProductController.php)
- Extend BaseControllerand add constructor with proper parent call
- Implement these standard callback methods:
- index()=> List all resources
- show()=> Display single resource
- create()=> Show creation form and handle POST
- edit()=> Show edit form
- update()=> Handle update POST
- delete()=> Handle resource deletion
 
Controller Constructor and Callback Methods Signatures
Section titled “Controller Constructor and Callback Methods Signatures”<?php
use Psr\Http\Message\ResponseInterface as Response;use Psr\Http\Message\ServerRequestInterface as Request;
class YourController extends BaseController  // e.g., ProductsController{    // Constructor    public function __construct(Container $container, private YourModel $model) // inject your model into the controller constructor    {        parent::__construct($container);    }
    // Signatures of controller methods: (callback methods)    public function index(Request $request, Response $response, array $args): Response    public function show(Request $request, Response $response, array $args): Response    public function create(Request $request, Response $response, array $args): Response    public function edit(Request $request, Response $response, array $args): Response    public function update(Request $request, Response $response, array $args): Response    public function delete(Request $request, Response $response, array $args): Response}Step 2: Create View Templates
Section titled “Step 2: Create View Templates”- Create a folder in app/Views/matching your resource (e.g.,products) name (lowercase) (if it doesn’t exist)
- Create these view files:
- index.php=> List view
- show.php=> Detail view
- create.php=> Creation form
- edit.php=> Edit form
 
Step 3: Register Routes
Section titled “Step 3: Register Routes”- Open app/Routes/web-routes.php
- Import your controller at the top with other use statements
- Add routes inside the return function using this pattern:
Route Naming Convention
Section titled “Route Naming Convention”Follow the pattern: [controller_name].[method_name]
Understanding URI Design and Resource Identification
Section titled “Understanding URI Design and Resource Identification”Why Use URIs to Identify Resources?
Section titled “Why Use URIs to Identify Resources?”URIs (Uniform Resource Identifiers) serve as unique addresses for your application’s resources. Think of them like street addresses - each resource needs a clear, predictable location.
Key Benefits:
- Predictability: Users and developers can guess URIs
- SEO-friendly: Search engines prefer meaningful URIs
- Maintainability: Consistent patterns make code easier to maintain
- REST Compliance: Follows web standards and best practices
Using RESTful URI Principles in your application
Section titled “Using RESTful URI Principles in your application”1. Use Nouns, Not Verbs
Section titled “1. Use Nouns, Not Verbs”✅ Good: /products (noun - represents the resource)
❌ Bad: /getProducts (verb - describes the action)
2. Use Plural Nouns for Collections
Section titled “2. Use Plural Nouns for Collections”✅ Good: /products (collection of products)
❌ Bad: /product (ambiguous - single or multiple?)
3. Use Hierarchical Structure
Section titled “3. Use Hierarchical Structure”✅ Good: /products/123/reviews (reviews for product 123)
❌ Bad: /reviews?product_id=123 (less intuitive)
4. Be Consistent with Casing
Section titled “4. Be Consistent with Casing”✅ Good: /products/categories (all lowercase)
❌ Bad: /Products/Categories (mixed case)
Standard CRUD URI Patterns
Section titled “Standard CRUD URI Patterns”| Action | HTTP Method | URI Pattern | Example | 
|---|---|---|---|
| List all | GET | /resources | /products | 
| Show one | GET | /resources/{id} | /products/123 | 
| Create form | GET | /resources/create | /products/create | 
| Store new | POST | /resources | /products | 
| Edit form | GET | /resources/{id}/edit | /products/123/edit | 
| Update | POST | /resources/{id} | /products/123 | 
| Delete | POST | /resources/{id}/delete | /products/123/delete | 
Standard Web Routes (CRUD)
Section titled “Standard Web Routes (CRUD)”Add these routes to your web-routes.php file for complete CRUD functionality:
// List all resources (GET /resource-name) e.g., GET /products$app->get('/resource-name', [YourController::class, 'index'])    ->setName('resource.index');
// Show single resource (GET /resource-name/{id}) e.g., GET /products/1$app->get('/resource-name/{id}', [YourController::class, 'show'])    ->setName('resource.show');
// Create new resource (form + processing) (GET /resource-name/create) e.g., GET /products/create$app->get('/resource-name/create', [YourController::class, 'create'])    ->setName('resource.create');// Create new resource (POST /resource-name) e.g., POST /products$app->post('/resource-name', [YourController::class, 'create']);
// Edit existing resource (form + processing) (GET /resource-name/{id}/edit) e.g., GET /products/1/edit$app->get('/resource-name/{id}/edit', [YourController::class, 'edit'])    ->setName('resource.edit');// Edit existing resource (POST /resource-name/{id}) e.g., POST /products/1$app->post('/resource-name/{id}', [YourController::class, 'update']);
// Delete resource (POST /resource-name/{id}/delete) e.g., POST /products/1/delete$app->post('/resource-name/{id}/delete', [YourController::class, 'delete'])    ->setName('resource.delete');Step 4: Create the Model
Section titled “Step 4: Create the Model”- Create a model class in app/Domain/Models/(e.g.,ProductsModel.php) (if it doesn’t exist)
- Implement methods for data operations in the model:
- getAll()=> Retrieve all records
- getById($id)=> Get single record
- create($data)=> Insert new record
- update($id, $data)=> Update existing record
- delete($id)=> Remove record
 
- Follow the MVC pattern - models handle data, controllers coordinate, views display
Step 5: Implement Controller Logic
Section titled “Step 5: Implement Controller Logic”- Inject your model into the controller constructor (if not already done)
- Use the model methods for all data operations
- Use $this->render()to display views with data from models
- Use $this->redirect()for navigation after form submissions
- Extract route parameters using $args['parameter_name']
- Get form data using $request->getParsedBody()
- Validate input data before passing to model methods
- Handle errors gracefully with appropriate redirects
Step 6: Test Your Routes
Section titled “Step 6: Test Your Routes”- Start your development server
- Visit each route in your browser
- Test form submissions
- Verify redirects work correctly
- Check error handling
Important Notes
Section titled “Important Notes”- Route Order Matters: Place specific routes (like /create) before parametric routes (like/{id})
- Named Routes: Always use ->setName()for routes you’ll redirect to
- URI Args vs Query Params: Use $uri_argsonly for routes with{placeholders}, use$query_paramsfor?key=value
- POST-Redirect-GET: Always redirect after successful form submissions to prevent duplicate submissions
Example File Structure
Section titled “Example File Structure”After following these steps, you should have:
app/├── Controllers/│   └── YourController.php   👉 (e.g., ProductsController.php)├── Models/│   └── YourModel.php        👉 (e.g., ProductsModel.php)├── Views/│   └── your-resource/       👉 (e.g., products)│       ├── index.php│       ├── show.php│       ├── create.php│       └── edit.php└── Routes/    └── web-routes.php (updated)MVC Pattern Reminder
Section titled “MVC Pattern Reminder”Remember to follow the MVC pattern throughout your implementation:
- Models: Handle all data operations, database queries, and business logic
- Views: Display data and handle user interface
- Controllers: Coordinate between models and views, handle HTTP requests/responses
Next Steps
Section titled “Next Steps”- Add validation to your controller methods
- Implement proper model classes with database operations
- Add error handling and user feedback
- Style your views with CSS
- Add authentication/authorization if needed
- Consider implementing advanced search features (autocomplete, faceted search)
- Add export functionality for filtered results