Laravel API Simulator

Controller
Response

        

What Is the Laravel API Simulator?

This tool lets you write PHP code that mimics Laravel-style API controller patterns and test JSON request/response handling directly in your browser. You provide a JSON request body, write a PHP handler that processes it, and see the JSON response output instantly without installing Laravel or running a local server.

The simulator focuses on the data-processing logic inside a Laravel controller method. You can parse incoming JSON with json_decode(), transform the data using PHP arrays, and print a formatted JSON response with json_encode(). This is the same logic you would place inside a controller method that returns response()->json() in a real Laravel application.

All code runs on a remote sandboxed server powered by the Piston API. Your browser sends the PHP source code and the JSON request body to the server, which executes the script inside an isolated container and returns the printed output as the simulated API response.

How It Works

  1. Write your PHP controller logic in the editor panel. The code reads a JSON request body from standard input, processes the data, and prints a JSON response to standard output.
  2. Enter a JSON request body in the Request Body field. This simulates the payload that would be sent to a Laravel API endpoint.
  3. Add route arguments in the Route Args field if your handler expects route parameters such as a user ID or resource slug.
  4. Click the Run button to send your code and input to the execution server. The server runs the PHP script with your JSON body provided through standard input.
  5. View the response in the Response panel. The output shows whatever your handler prints, which is typically a formatted JSON response object.

Step-by-Step Example

Suppose you want to simulate a Laravel controller endpoint that accepts a product name and quantity, then returns an order confirmation. Here is how to do it:

First, type the following code into the editor panel:

<?php
$body = trim(stream_get_contents(STDIN));
$data = json_decode($body, true) ?? [];

$product = $data["product"] ?? "unknown";
$quantity = intval($data["quantity"] ?? 1);
$total = $quantity * 9.99;

$response = [
    "status" => "ok",
    "order" => [
        "product" => $product,
        "quantity" => $quantity,
        "total" => number_format($total, 2)
    ],
    "framework" => "Laravel"
];

echo json_encode($response, JSON_PRETTY_PRINT);

Next, enter the following JSON in the Request Body field:

{"product": "Widget", "quantity": 3}

Now click the Run button. The server executes your PHP handler with the provided JSON body. After a moment, the Response panel displays a JSON object with the order confirmation including the calculated total.

You can modify the request body or the controller logic and run again to test different scenarios, just as you would iterate on a real Laravel controller method.

Use Cases

Limitations and Notes

Frequently Asked Questions

Does this run a real Laravel server?

No. This is a simulation that runs PHP code to mimic Laravel-style controller patterns. No actual Laravel framework or Artisan server is started.

Does Eloquent ORM work here?

No. Eloquent ORM and database connections are not available. You can simulate data using PHP arrays and objects but cannot query a real database.

Does Laravel routing work?

Routing is shown as a simulated pattern for learning purposes. The tool does not process actual Laravel route definitions or middleware pipelines.

Can I use Blade templates?

No. Blade template rendering is not supported. This tool focuses on JSON API response patterns rather than HTML view rendering.

Can I install Composer packages?

No. Composer and third-party packages are not available. Only PHP's built-in functions and standard library can be used.

Does middleware work?

No. Laravel middleware pipelines are not available in the sandbox. You write plain PHP handler logic without middleware processing.

What is the main use case for this tool?

It is designed for learning Laravel-style API controller patterns, practicing JSON request and response handling in PHP, and understanding how controllers process data.

Can I return JSON responses?

Yes. You can use json_encode() to construct and print JSON responses, simulating the output a Laravel controller would return with response()->json().

Sources and References

Related Compilers