Field Manual Brokerage / Linux / Laravel
← Laravel

bootstrap/app.php

Fresh Laravel 11/12 skeleton: replace. In an existing app merge the exception handlers and preserve routing, middleware and providers.

What it is

Complete brokerage practice file. The Assemble project page lists every file in order.

When to use it

Fresh Laravel 11/12 skeleton: replace. In an existing app merge the exception handlers and preserve routing, middleware and providers.

Syntax

mkdir -p 'bootstrap'
# Paste the Example below into bootstrap/app.php

Example

<?php

declare(strict_types=1);

use App\Exceptions\ExchangeException;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware): void {
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        $exceptions->render(function (ExchangeException $exception, Request $request): JsonResponse {
            return response()->json(['message' => $exception->getMessage()], $exception->httpStatus);
        });
        $exceptions->render(function (Throwable $exception, Request $request): ?JsonResponse {
            if ($request->expectsJson()
                && !$exception instanceof ExchangeException
                && !$exception instanceof ValidationException
                && !$exception instanceof HttpExceptionInterface) {
                return response()->json(['message' => 'An unexpected error occurred. Please contact the broker before submitting again.'], 500);
            }
            return null;
        });
    })->create();

Common mistakes

Use the setup runbook first. Adapt provisional API assumptions to the supplied contract. Merge into supplied applications; do not duplicate the orders migration.

Related topics

API contract: change these points first