How to change response in Laravel?

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Dynamic Responses in Laravel: Controlling API Formats

As developers building modern RESTful services with Laravel, one of the most common requirements is the need for flexible endpoints. Users often don't want a single, fixed response format; they might prefer machine-readable JSON for programmatic access or rich HTML for direct rendering. This flexibility allows you to serve data efficiently across different client applications.

This post will guide you through implementing dynamic response formatting in your Laravel controllers, allowing you to control whether an endpoint returns JSON, HTML, or any other desired structure based on query parameters.

The Challenge: Dynamic Response Formatting

You have a standard RESTful service where requests like api/main return JSON data using the traditional method:

// Standard JSON response
return response()->json(["categories" => $categories]);

The challenge is extending this functionality so that you can modify this output based on user input, for instance, by appending a query parameter: api/main?format=json|html. We need the controller to inspect this parameter and execute different logic paths.

The Solution: Inspecting Request Parameters in Controllers

The key to solving this lies in accessing the incoming request data within your controller method. Laravel provides straightforward ways to access query parameters via the request() helper or the Illuminate\Http\Request object injected into the method.

Step 1: Accessing the Format Parameter

First, we retrieve the requested format from the URL. We will use the request()->query() method to get all incoming query strings.

Step 2: Implementing Conditional Logic

Next, we check if the desired format (format) exists and then use conditional statements (like if/elseif) to determine which response function to call.

Here is a practical example demonstrating how to implement this logic in a controller method:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DataController extends Controller
{
    public function getData(Request $request)
    {
        // 1. Get the requested format from the query parameters
        $format = $request->query('format', 'json'); // Default to json if not specified

        // 2. Fetch the core data (this part remains constant)
        $categories = [
            'electronics',
            'books',
            'clothing'
        ];

        // 3. Conditional Response Logic
        if ($format === 'html') {
            // If format is HTML, return a Blade view instead of JSON
            return view('data.categories', ['categories' => $categories]);
        } elseif ($format === 'json') {
            // If format is JSON, return the standard JSON response
            return response()->json(["categories" => $categories]);
        } else {
            // Handle unknown formats gracefully
            return response()->json(['error' => 'Invalid format specified. Use "json" or "html".']);
        }
    }
}

Best Practices for API Design in Laravel

When designing APIs, especially when dealing with varied output types, adhering to clean separation of concerns is crucial. While the example above works perfectly within a controller, for larger applications, consider using dedicated Response Classes or Action Classes. This keeps your controllers lean and focuses them purely on routing and orchestrating data flow, allowing specialized classes to handle complex response generation.

Furthermore, always prioritize clear error handling. As demonstrated, if an invalid parameter is provided, you should return a meaningful error message (e.g., HTTP 400 Bad Request) rather than crashing or returning unexpected data. Laravel's robust request handling makes implementing these conditional flows very straightforward, reinforcing how powerful the framework is for building scalable APIs, much like the architecture promoted by Laravel Company.

Conclusion

By leveraging Laravel’s request handling capabilities—specifically inspecting query parameters within your controller—you gain complete control over the response format of your RESTful endpoints. Whether you need to serve structured JSON data for mobile apps or render rich HTML content for web pages, conditional logic allows you to deliver exactly what the client expects. This dynamic approach ensures your API is versatile, adaptable, and highly functional for any front-end requirement.