Zum Inhalt springen

Laravel Proxy

Leiten Sie usertrax über Ihre Laravel-Anwendung, um Script und Events von Ihrer eigenen Domain auszuliefern.

  1. Proxy-Controller erstellen

    Terminal-Fenster
    php artisan make:controller UsertraxProxyController
  2. Proxy-Logik implementieren

    In app/Http/Controllers/UsertraxProxyController.php:

    <?php
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use Illuminate\Http\Response;
    use Illuminate\Support\Facades\Cache;
    use Illuminate\Support\Facades\Http;
    class UsertraxProxyController extends Controller
    {
    public function script(): Response
    {
    $body = Cache::remember('usertrax_script', 86_400, function (): string {
    return Http::get('https://usertrax.io/cvs.js')->body();
    });
    return response($body, 200, [
    'Content-Type' => 'application/javascript',
    'Cache-Control' => 'public, max-age=86400',
    ]);
    }
    public function events(Request $request): Response
    {
    $response = Http::withHeaders([
    'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'User-Agent' => $request->header('User-Agent', ''),
    'X-Auth-Key' => $request->header('X-Auth-Key', ''),
    ])->post('https://api.usertrax.io/api/events', $request->all());
    return response($response->body(), $response->status(), [
    'Content-Type' => 'application/json',
    ]);
    }
    }
  3. Routen registrieren

    In routes/web.php:

    use App\Http\Controllers\UsertraxProxyController;
    Route::get('/cvs.js', [UsertraxProxyController::class, 'script']);
    Route::post('/api/usertrax/events', [UsertraxProxyController::class, 'events']);

    Nutzen Sie web.php, nicht api.php, damit CSRF Browser-POST-Requests nicht blockiert.

  4. Blade-Layout anpassen

    <script>
    window.usertraxConfig = {
    endpoint: "/api/usertrax/events",
    };
    </script>
    <script src="/cvs.js" data-key="{{ config('services.usertrax.key') }}" defer></script>
  5. Deployen

    Nach dem Deployment ist der Proxy aktiv. Bei Route-Cache: php artisan route:clear.

  1. Website besuchen
  2. Network-Tab öffnen
  3. Prüfen, dass Anfragen über Ihre Domain laufen
  • 419 CSRF token mismatch: Events-Route in web.php platzieren oder von CSRF ausschließen
  • 401 Unauthorized: X-Auth-Key-Weiterleitung und data-key prüfen