Laravel Example: Defining Routes for an E-commerce Platform

Updated: January 18, 2024 By: Guest Contributor Post a comment

Introduction

Building an e-commerce platform with Laravel is an exciting task for any developer. One of the first requirements you’ll encounter is the need to define your routes clearly and efficiently. Routing in Laravel is the mechanism by which you direct HTTP requests to controllers and their functions. It’s essential for crafting a responsive, organized web application. In this tutorial, we’ll walk through the process of defining routes for a basic e-commerce platform in Laravel.

Understanding Laravel Routing

In Laravel, all routes are defined in the routes directory, within the web.php file for web requests. Routes point to controller actions, closures, or directly return views.


 // A basic GET route
 Route::get('/products', function () {
     return view('products.index');
 });
 

Setting Up Our E-commerce Platform’s Routing

We’ll start by assuming you’ve already set up your Laravel installation, along with a basic set of views and controllers specific to an e-commerce site. We’ll focus on a few routes that are key to any e-commerce platform:

  • Viewing products
  • Product details page
  • Shopping cart
  • Checkout process
  • User authentication

Viewing Products

Let’s begin with the route that displays all of our products:


 Route::get('/products', 'ProductController@index');
 

When a GET request is made to /products, the index function of the ProductController is called. Here’s what this function might look like:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;

class ProductController extends Controller
{
   public function index()
   {
      $products = Product::all();
      return view('products.index', compact('products'));
   }
}

Product Details Page

A key part of any shopping experience is the product details page. Follow this route:


 Route::get('/products/{id}', 'ProductController@show');
 

This route captures a parameter from the URL—{id}—which is then passed to the show method to retrieve the corresponding product.


 public function show($id)
 {
     $product = Product::findOrFail($id);
     return view('products.show', compact('product'));
 }
 

Shopping Cart

Next, let’s tackle the shopping cart:


 Route::get('/cart', 'CartController@index');
 Route::post('/cart', 'CartController@store');
 Route::delete('/cart/{product}', 'CartController@destroy');
 

We’ve defined three actions here: viewing the cart, adding an item to the cart, and removing an item from the cart.

Checkout Process

The checkout process usually involves multiple steps, therefore, it is optimal to group these routes.


 Route::get('/checkout', 'CheckoutController@index');
 Route::post('/checkout', 'CheckoutController@store');
 Route::get('/checkout/success', 'CheckoutController@success');
 

No e-commerce platform is complete without user accounts. For users to manage their addresses, orders, and preferences, you’ll need authentication routes:


 Auth::routes();
 

This function from Laravel’s built-in Auth facade automatically sets up the standard authentication routes for login, registration, logout, etc.

Route Grouping & Middleware

For better organization and control, we should group related routes. For example, the cart and checkout routes should be accessible only to authenticated users:


 Route::group(['middleware' => ['auth']], function () {
     Route::get('/cart', 'CartController@index');
     Route::post('/cart', 'CartController@store');
     Route::delete('/cart/{product}', 'CartController@destroy');
     Route::get('/checkout', 'CheckoutController@index');
     Route::post('/checkout', 'CheckoutController@store');
     Route::get('/checkout/success', 'CheckoutController@success');
 });
 

Resource Routes

Some controllers, like the ProductController, often map to basic CRUD operations. We can simplify route definitions using resource controllers:


 Route::resource('products', 'ProductController');
 

This one line generates all necessary CRUD routes aligned with the controller’s methods like index, create, store, show, edit, update, and destroy.

Conclusion

In this guide, we’ve outlined how to define routes for a basic e-commerce platform using Laravel. While our examples are straightforward, the principles of route declaration, controller mapping, and group middleware hold constant as your application scales. With Laravel’s powerful routing capabilities, you can create a user-friendly e-commerce experience that scales alongside your business.