Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Michalfrantisek's avatar

Laravel 11: Single Telemetry Endpoint for Redirection (POST Requests)

Description:

I'm building an API in Laravel 11 that receives telemetry data of various types. While I have dedicated endpoints for processing each specific telemetry type (all supporting only POST requests), I'd like to have a single, central endpoint to initially receive and then redirect the request to the appropriate handler based on telemetry data.

Challenge:

The traditional redirect methods (e.g., redirect()->route()) don't preserve the original POST request method. This poses a problem since my specific handlers all expect POST requests.

What I've Tried:

I attempted using redirect()->route() but this changes the request method to GET. Question:

Is there a recommended approach in Laravel 11 to achieve this central redirection while maintaining the POST request method? Would utilizing middleware be a suitable solution? If so, could you provide some guidance on how to implement it? Are there any alternative approaches I should consider for this scenario?

0 likes
2 replies
LaryAI's avatar
Level 58

To achieve a central endpoint that redirects POST requests to the appropriate handler while preserving the POST method in Laravel 11, you can use a combination of a controller and middleware. The middleware can be used to determine the appropriate handler based on the telemetry data and then forward the request internally.

Here's a step-by-step solution:

  1. Create a Central Controller: This controller will receive the initial POST request and determine the appropriate handler.

  2. Create Middleware: This middleware will inspect the telemetry data and forward the request to the correct handler.

  3. Register Middleware: Ensure the middleware is registered in your Kernel.php.

  4. Define Routes: Define the central endpoint and the specific handlers in your web.php or api.php routes file.

Step 1: Create a Central Controller

Create a controller named TelemetryController:

php artisan make:controller TelemetryController

In TelemetryController.php:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TelemetryController extends Controller
{
    public function handle(Request $request)
    {
        // Here you can add logic to determine the type of telemetry data
        // For example, let's assume the type is in the 'type' field of the request
        $type = $request->input('type');

        // Forward the request to the appropriate handler
        return app()->make('App\Http\Controllers\TelemetryHandlerController')->$type($request);
    }
}

Step 2: Create Middleware

Create middleware named RedirectTelemetry:

php artisan make:middleware RedirectTelemetry

In RedirectTelemetry.php:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class RedirectTelemetry
{
    public function handle(Request $request, Closure $next)
    {
        // Determine the type of telemetry data
        $type = $request->input('type');

        // Forward the request to the appropriate handler
        if ($type) {
            return app()->make('App\Http\Controllers\TelemetryHandlerController')->$type($request);
        }

        return $next($request);
    }
}

Step 3: Register Middleware

In app/Http/Kernel.php, register the middleware:

protected $routeMiddleware = [
    // Other middleware
    'redirect.telemetry' => \App\Http\Middleware\RedirectTelemetry::class,
];

Step 4: Define Routes

In routes/api.php or routes/web.php:

use App\Http\Controllers\TelemetryController;
use App\Http\Controllers\TelemetryHandlerController;

Route::post('/telemetry', [TelemetryController::class, 'handle'])->middleware('redirect.telemetry');

// Define specific handlers
Route::post('/telemetry/type1', [TelemetryHandlerController::class, 'type1']);
Route::post('/telemetry/type2', [TelemetryHandlerController::class, 'type2']);
// Add more handlers as needed

Step 5: Create TelemetryHandlerController

Create a controller named TelemetryHandlerController:

php artisan make:controller TelemetryHandlerController

In TelemetryHandlerController.php:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TelemetryHandlerController extends Controller
{
    public function type1(Request $request)
    {
        // Handle type1 telemetry data
        return response()->json(['message' => 'Handled type1']);
    }

    public function type2(Request $request)
    {
        // Handle type2 telemetry data
        return response()->json(['message' => 'Handled type2']);
    }

    // Add more handlers as needed
}

Summary

This setup allows you to have a central endpoint that receives POST requests and forwards them to the appropriate handler based on the telemetry data type. The middleware inspects the request and forwards it internally, preserving the POST method. This approach ensures that your specific handlers receive the request as expected.

Michalfrantisek's avatar
Michalfrantisek
OP
Best Answer
Level 11

@michalfrantisek

I was able to find my best solution. Here is a code snippet.

class ProcessTelemetryController extends Controller
{
    /**
     * Handle the incoming request.
     */
    public function __invoke(Request $request)
    {
        if ($this->isMeratchTelemetry($request)) {
            return redirect(
                to: route('api.sensor.meratch.process-telemetry'),
                status: Response::HTTP_TEMPORARY_REDIRECT,
            );
        }

        if ($this->isMeratchPlsTelemetry($request)) {
            return redirect(
                to: route('api.sensor.meratch-pls.process-telemetry'),
                status: Response::HTTP_TEMPORARY_REDIRECT,
            );
        }

        abort(
            Response::HTTP_NOT_FOUND,
            'Unknown telemetry data. Supported devices are: ' . DeviceType::MERATCH->getDescription() . ', ' . DeviceType::MERATCH_PLS->getDescription()
        );
    }
1 like

Please or to participate in this conversation.