Feature: Middleware #333

Closed
opened 2025-03-28 00:05:50 +00:00 by jamie · 4 comments
jamie commented 2025-03-28 00:05:50 +00:00 (Migrated from git.hazaar.io)

Problem Statement

Hazaar Framework currently lacks a middleware system, making it difficult for developers to implement reusable logic that can be executed at different stages of the request lifecycle. This results in more boilerplate code in controllers and routes, reducing maintainability and flexibility.

Who will benefit?

  • Developers who want to implement cross-cutting concerns such as authentication, logging, request validation, and response modifications.
  • Applications that require reusable logic across multiple routes or controllers.
  • Projects that need a structured way to hook into the request lifecycle.

Benefits and risks

Benefits:

  • Provides a structured way to handle cross-cutting concerns.
  • Reduces repetitive code in controllers and routes.
  • Improves code maintainability and reusability.
  • Allows for more flexible request handling.

Risks:

  • Could introduce performance overhead if middleware is not optimized.
  • Requires careful implementation to prevent conflicts between middleware layers.
  • Developers migrating from Hazaar MVC will need to adapt to the new middleware structure.

Proposed solution

Introduce a middleware system with three types of middleware:

  1. Global Middleware: Executed while the application is initializing, after the request is processed but before the route is loaded.
  2. Route Middleware: Assigned to a specific route and executed before handling the request.
  3. Controller Middleware: Attached to controllers and executed either before a controller action runs or after it completes.

Middleware should be defined in the app/Middleware directory and registered through a configuration file.

Examples

  • Laravel’s middleware system follows a similar approach, where middleware can be applied globally, to routes, or to controllers.
  • Express.js in Node.js uses a middleware pattern that allows function chaining before handling a request.

Priority/Severity

  • High (This will bring a huge increase in performance/productivity/usability/legislative cover)
  • Medium (This will bring a good increase in performance/productivity/usability)
  • Low (anything else e.g., trivial, minor improvements)
## Problem Statement Hazaar Framework currently lacks a middleware system, making it difficult for developers to implement reusable logic that can be executed at different stages of the request lifecycle. This results in more boilerplate code in controllers and routes, reducing maintainability and flexibility. ## Who will benefit? - Developers who want to implement cross-cutting concerns such as authentication, logging, request validation, and response modifications. - Applications that require reusable logic across multiple routes or controllers. - Projects that need a structured way to hook into the request lifecycle. ## Benefits and risks ### Benefits: - Provides a structured way to handle cross-cutting concerns. - Reduces repetitive code in controllers and routes. - Improves code maintainability and reusability. - Allows for more flexible request handling. ### Risks: - Could introduce performance overhead if middleware is not optimized. - Requires careful implementation to prevent conflicts between middleware layers. - Developers migrating from Hazaar MVC will need to adapt to the new middleware structure. ## Proposed solution Introduce a middleware system with three types of middleware: 1. **Global Middleware**: Executed while the application is initializing, after the request is processed but before the route is loaded. 2. **Route Middleware**: Assigned to a specific route and executed before handling the request. 3. **Controller Middleware**: Attached to controllers and executed either before a controller action runs or after it completes. Middleware should be defined in the `app/Middleware` directory and registered through a configuration file. ## Examples - Laravel’s middleware system follows a similar approach, where middleware can be applied globally, to routes, or to controllers. - Express.js in Node.js uses a middleware pattern that allows function chaining before handling a request. ## Priority/Severity - [x] High (This will bring a huge increase in performance/productivity/usability/legislative cover) - [ ] Medium (This will bring a good increase in performance/productivity/usability) - [ ] Low (anything else e.g., trivial, minor improvements)
jamie commented 2025-03-28 00:05:50 +00:00 (Migrated from git.hazaar.io)

assigned to @jamie

assigned to @jamie
jamie commented 2025-07-11 02:21:48 +00:00 (Migrated from git.hazaar.io)

created branch 333-feature-middleware to address this issue

created branch [`333-feature-middleware`](/hazaar/framework/-/compare/master...333-feature-middleware) to address this issue
jamie commented 2025-07-11 02:21:53 +00:00 (Migrated from git.hazaar.io)

mentioned in merge request !233

mentioned in merge request !233
jamie commented 2025-07-23 05:26:00 +00:00 (Migrated from git.hazaar.io)

mentioned in commit 90b09d2951

mentioned in commit 90b09d29514cdd2645df7d7012eaa43dfa1e235f
jamie (Migrated from git.hazaar.io) closed this issue 2025-07-23 05:26:02 +00:00
jamie self-assigned this 2025-09-04 01:13:36 +00:00
jamie removed their assignment 2025-09-04 01:13:44 +00:00
jamie self-assigned this 2025-09-04 01:13:48 +00:00
jamie removed their assignment 2025-09-04 01:13:54 +00:00
jamie self-assigned this 2025-09-04 01:14:01 +00:00
jamie removed their assignment 2025-09-04 01:14:06 +00:00
jamie self-assigned this 2025-09-04 01:14:22 +00:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: hazaar/framework#333
No description provided.