Engineering note · Laravel

Simplify Routing with Route Groups, Prefixes, and Middleware

By Published

Laravel

To organize routes efficiently using route groups with prefixes and middleware, making code cleaner and easier to manage.

CODE SAMPLE Code snippet
// Define a route group with a prefix and middleware
Route::prefix('admin')
    ->middleware('auth')
    ->group(function () {
        Route::get('dashboard', [AdminController::class, 'dashboard']);
        Route::get('settings', [AdminController::class, 'settings']);
    });

Route::prefix('admin'): Adds the 'admin' prefix to all routes in this group, so they will be like admin/dashboard and admin/settings.

->middleware('auth'): Applies the 'auth' middleware to all routes in this group, meaning users must be authenticated to access these routes.

->group(function () { ... }): Groups multiple routes together, making it easier to apply common settings like prefixes and middleware.

More engineering notes

Handling Dates with Carbon Date Helpers

Carbon Helper simplifies working with dates, making it easier to perform common tasks like formattin...

Use Lazy Eager Loading for Conditional Relationships

Load related models only when needed using lazy eager loading. This technique helps in optimizing qu...