Resource controllers follow RESTful conventions for handling CRUD (Create, Read, Update, Delete) operations. While Aeros doesn’t have a dedicated resource controller generator, you can easily structure your controllers to follow REST principles.
Create a resource controller that follows REST conventions:
PhotoController.php
<?phpnamespace App\Controllers;use Aeros\Src\Classes\Controller;use Aeros\Src\Classes\Response;class PhotoController extends Controller{ /** * Display a listing of the resource. */ public function index() { $photos = db()->query('SELECT * FROM photos ORDER BY created_at DESC'); return view('photos.index', ['photos' => $photos]); } /** * Show the form for creating a new resource. */ public function create() { return view('photos.create'); } /** * Store a newly created resource in storage. */ public function store() { $validated = validate([ 'title' => 'required|string|max:255', 'description' => 'string', 'image' => 'required|image' ]); // Handle file upload $uploaded = store(request('files')['image'], '/uploads/photos'); db()->insert('photos', [ 'title' => $validated['title'], 'description' => $validated['description'] ?? '', 'path' => $uploaded[0]['path'], 'created_at' => date('Y-m-d H:i:s') ]); return redirect('/photos'); } /** * Display the specified resource. */ public function show($id) { $photo = db()->query('SELECT * FROM photos WHERE id = ?', [$id])[0] ?? null; if (!$photo) { abort('Photo not found', 404); } return view('photos.show', ['photo' => $photo]); } /** * Show the form for editing the specified resource. */ public function edit($id) { $photo = db()->query('SELECT * FROM photos WHERE id = ?', [$id])[0] ?? null; if (!$photo) { abort('Photo not found', 404); } return view('photos.edit', ['photo' => $photo]); } /** * Update the specified resource in storage. */ public function update($id) { $validated = validate([ 'title' => 'required|string|max:255', 'description' => 'string' ]); db()->update('photos', $validated, ['id' => $id]); return redirect('/photos/' . $id); } /** * Remove the specified resource from storage. */ public function destroy($id) { $photo = db()->query('SELECT * FROM photos WHERE id = ?', [$id])[0] ?? null; if ($photo && file_exists($photo['path'])) { unlink($photo['path']); } db()->delete('photos', ['id' => $id]); return redirect('/photos'); }}
Set up routes that map to your resource controller methods:
routes/web.php
use Aeros\Src\Classes\Router;// Display all photosRouter::get('/photos', 'PhotoController@index');// Show create formRouter::get('/photos/create', 'PhotoController@create');// Store new photoRouter::post('/photos', 'PhotoController@store');// Show specific photoRouter::get('/photos/{id}', 'PhotoController@show');// Show edit formRouter::get('/photos/{id}/edit', 'PhotoController@edit');// Update photoRouter::put('/photos/{id}', 'PhotoController@update');// Alternative: Router::patch('/photos/{id}', 'PhotoController@update');// Delete photoRouter::delete('/photos/{id}', 'PhotoController@destroy');
Make sure to define the /photos/create route before/photos/{id} to prevent “create” from being interpreted as an ID parameter.