Admin Panel
What is Admin Panel?
Section titled โWhat is Admin Panel?โAdmin Panel is a special, protected area of your website that only administrators can access. Think of it like the โbackstageโ of your e-commerce site, where admins can manage products, orders, categories, and other important website content.
Why Do We Need It?
Section titled โWhy Do We Need It?โIn a real e-commerce application, you donโt want regular users to be able to add or delete products, or view all orders. Thatโs why we create an admin panel:
- Protect sensitive data: Only admins can access it.
- Manage content: Admins can add, update, or remove products and categories.
- Monitor orders: Admins can view and manage customer orders.
- Control the site: Admins have special permissions regular users donโt have.
Step-by-Step Implementation
Section titled โStep-by-Step Implementationโ1. Create Admin Authentication Middleware
Section titled โ1. Create Admin Authentication MiddlewareโWhat is Middleware? Middleware acts like a โsecurity guardโ for your admin routes. Before any admin page loads, the middleware checks: โIs this user logged in? Are they an admin?โ If not, it redirects them away.
What Your Middleware Should Do:
- Check if the user is logged in (authenticated).
- Check if the user has admin privileges (is an admin).
- If both conditions are met โ allow access to the admin page.
- If either check fails โ redirect to the login page.
Think of it like this: Middleware runs BEFORE your controllerโs request handler (callback method), acting as a gatekeeper. A middleware is a reusable component, so you donโt have to write the same security checks in every admin controller.
2. Create a Set of Controllers
Section titled โ2. Create a Set of ControllersโA set of controller classes is required to be created to handle the โbusiness logicโ for your admin pages. Each controller class should manage a specific part of your admin panel.
Common Admin Controllers:
- AdminController or DashboardController: Shows an overview/stats (total products, orders, users, etc.).
- ProductsController: Handles listing, creating, editing, and deleting products.
- CategoriesController: Manages product categories.
- OrdersController: Views and manages customer orders.
- UsersController: Manages user accounts and permissions.
Each controller should have methods like:
- index()โ Display a list of items.
- show()โ Show details of a single item.
- create()โ Display a form to create a new item.
- store()โ Save a new item to the database.
- edit()โ Display a form to edit an item.
- update()โ Save changes to an item.
- delete()โ Remove an item.
All the logic for handling admin requests goes in these controllers.
3. Set Up an Admin Routes Group
Section titled โ3. Set Up an Admin Routes GroupโWhat is a Route Group? A route group lets you organize related routes and apply the one ore middleware to all of them at once. Itโs like grouping all your admin routes under one โumbrellaโ and saying โall these routes are protected.โ
Why Group Admin Routes?
- All admin routes share the same prefix (/admin).
- All admin routes need the same security check (AdminAuthMiddleware).
- It keeps your code organized and DRY (Donโt Repeat Yourself).
Routing Group Example
Section titled โRouting Group Exampleโ$app->group('/admin', function ($group) {    // Dashboard route    $group->get('/dashboard', [AdminController::class, 'dashboard']);
    // User management routes    $group->get('/users', [AdminController::class, 'users']);
    // Product management routes    $group->get('/products', [ProductsController::class, 'index']);    $group->post('/products/create', [ProductsController::class, 'createProduct']);
    // Category management routes    $group->get('/categories', [CategoriesController::class, 'index']);    $group->post('/categories/create', [CategoriesController::class, 'create']);
    // TODO: Add here the remaining routes for orders, product editing, category editing, etc.})->add(AdminAuthMiddleware::class);  // Apply middleware to ALL routes in this groupHow This Works:
- All routes inside the group automatically start with /admin
- The middleware at the end (.add(AdminAuthMiddleware::class)) applies to every route in the group
- When someone tries to visit /admin/products, the middleware runs first to check if theyโre an admin
Routes Created:
- /admin/dashboardโ Admin dashboard
- /admin/usersโ User management
- /admin/productsโ Product listing
- /admin/products/createโ Create new product (POST request)
- /admin/categoriesโ Category listing
- /admin/categories/createโ Create new category (POST request)
4. Create Admin Views
Section titled โ4. Create Admin ViewsโWhat Are Views? Views are the HTML/UI that users see. They display the data from your controllers and provide forms for admins to interact with (add products, edit categories, etc.).
Views Youโll Need:
- Dashboard View โ Shows statistics and overview of your e-commerce site.
- Products Views โ List products, show details, create/edit product forms.
- Categories Views โ Manage categories with forms to add/edit.
- Orders Views โ Display and manage customer orders.
- Users Views โ Manage user accounts and admin privileges.
Tips for Admin Views:
- Keep them consistent with clean, professional styling (e.g., using Bootstrap, Bluma, Tailwind CSS, etc.).
- Make forms clear and easy to use.
- Add confirmation dialogs for delete actions.
- Show success/error messages to users.
- Include navigation menu to switch between different admin sections.
Expected File Structure
Section titled โExpected File StructureโAfter implementing the admin panel following the steps above, your project should be organized like this:
app/โโโ Controllers/โ   โโโ AdminController.phpโ   โโโ DashboardController.phpโ   โโโ UsersController.phpโ   โโโ ProductsController.phpโ   โโโ OrdersController.phpโโโ Models/โ   โโโ AdminModel.phpโ   โโโ DashboardModel.phpโ   โโโ UsersModel.phpโ   โโโ ProductsModel.phpโ   โโโ OrdersModel.phpโโโ Views/โ   โโโ admin/โ       โโโ dashboardView.phpโ       โโโ usersView.phpโ       โโโ orders/โ       โ   โโโ orderIndexView.phpโ       โ   โโโ orderShowView.phpโ       โโโ products/โ       โ   โโโ productIndexView.phpโ       โ   โโโ productShowView.phpโ       โ   โโโ productCreateView.phpโ       โ   โโโ productEditView.phpโ       โโโ categories/โ       โ    โโโ categoryIndexView.phpโ       โ    โโโ categoryShowView.phpโ       โ    โโโ categoryCreateView.phpโ       โ    โโโ categoryEditView.phpโโโ Views/โ       โโโ cart/โ       โ   โโโ cartItemsView.phpโ       โ   โโโ cartSummaryView.phpโ       โ   โโโ checkoutFormView.phpโ       โ   โโโ checkoutConfirmationView.phpโโโ Routes/    โโโ web-routes.php