Skip to content

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.


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.

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.


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.


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).
$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 group

How This Works:

  1. All routes inside the group automatically start with /admin
  2. The middleware at the end (.add(AdminAuthMiddleware::class)) applies to every route in the group
  3. 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)

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.

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