پرش به مطلب اصلی

V3 Routes Adaptor Guide

Introduction

The V3RoutesAdaptor utility enables combining multiple V3 controllers within a single route and displaying their outputs in a unified view. This utility is designed with a Single Page Application (SPA) mindset, facilitating future migrations to frameworks like React or Vue.

Key Benefit

This approach allows for modular controller organization while maintaining a unified frontend experience, making future transitions to modern JavaScript frameworks much smoother.

Basic Usage

To use this utility, call the createRoute method in your mg_route file as follows:

routes/mg_route.php
use App\Utilities\V3RoutesAdaptor;
use App\Utilities\V3RouteControllerCollection;

V3RoutesAdaptor::createRoute(
method: 'GET', // HTTP method
controllers: new V3RouteControllerCollection([...]), // Controller collection
url: 'your/route/path', // Route path
viewPath: 'path.to.your.view' // View path
)

Parameters

method: 'GET'

The HTTP method for the route. Common values include:

  • GET - For retrieving data
  • POST - For creating new resources
  • PUT - For updating existing resources
  • DELETE - For removing resources

This parameter determines how the route will respond to different types of HTTP requests.

Defining Controllers with V3RouteControllerDTO

Each controller must be defined using a V3RouteControllerDTO object, which includes the following properties:

Properties Explained

  • controller: The controller class name (with namespace)
  • controllerMethod: The controller method to be called
  • controllerMethodArgs: (Optional) A closure that returns an array of arguments to be passed to the controller method
  • v3Params: (Optional) A closure that returns additional parameters to modify the request

Usage Examples

Simple List Display

This is the simplest use case, calling the index method of a controller without any parameters:

Basic Controller Example
use App\Utilities\V3RouteControllerDTO;
use App\Http\Controllers\UserController;

new V3RouteControllerDTO(
controller: UserController::class,
controllerMethod: 'index'
)

This will call the index method of the UserController class with no additional parameters.

How It Works

When a route created with V3RoutesAdaptor is accessed:

  1. The system processes each controller in the collection sequentially
  2. Results from all controllers are merged into a single data array
  3. The combined data is passed to the specified Blade view
  4. The view renders the complete page with all the data

This approach maintains separation of concerns while providing a unified user experience.

Best Practices

  • Organize by Domain: Group controllers by domain or feature rather than by technical function
  • Limit Controller Count: Try to keep the number of controllers per route reasonable (3-5 max) for maintainability
  • Use Descriptive Names: Name your controller methods clearly to indicate their purpose
  • Consider Performance: Be mindful of the performance impact of executing multiple controllers for a single route
  • Handle Errors Consistently: Implement consistent error handling across all controllers

Conclusion

The V3RoutesAdaptor utility provides a powerful way to combine multiple controllers in a single route, facilitating a more modular and maintainable codebase while preparing for potential future migrations to modern JavaScript frameworks.