← Integrations

Backend · PHP

A Western natal chart page in Laravel

The Laravel quickstart gets a first call working. This page is the next step: a form-backed controller for a tropical natal chart, with the house system as a user choice, a forever cache and a Blade table - plus the two values PHP forms get wrong.

Updated

How this page was checked: The PHP was not executed - there is no PHP runtime on the build machine. The request it builds (same URL, headers and JSON body) was sent with curl to the in-memory API; the chart values, the whole_sign 400 and the string-number 400 are from those runs.

The controller

Config is the same as the quickstart: services.astronode.key and services.astronode.base_url, read from .env in config/services.php.

php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Validation\Rule;

class NatalChartController extends Controller
{
    public function __invoke(Request $request)
    {
        $v = $request->validate([
            'date'              => ['required', 'date_format:Y-m-d'],
            'time'              => ['required', 'regex:/^\d{2}:\d{2}(:\d{2})?$/'],
            'tz_offset_minutes' => ['required', 'integer', 'between:-840,840'],
            'lat'               => ['required', 'numeric', 'between:-90,90'],
            'lng'               => ['required', 'numeric', 'between:-180,180'],
            'house_system'      => ['nullable', Rule::in(['placidus', 'koch', 'equal', 'whole-sign', 'porphyry'])],
        ]);

        // Form posts arrive as strings; the API wants JSON numbers.
        $body = [
            'date'              => $v['date'],
            'time'              => $v['time'],
            'tz_offset_minutes' => (int) $v['tz_offset_minutes'],
            'lat'               => (float) $v['lat'],
            'lng'               => (float) $v['lng'],
            'house_system'      => $v['house_system'] ?? 'placidus',
        ];

        $chart = Cache::rememberForever('natal:' . hash('sha256', json_encode($body)), function () use ($body) {
            return Http::withToken(config('services.astronode.key'))
                ->baseUrl(config('services.astronode.base_url'))
                ->acceptJson()
                ->timeout(15)
                ->retry(2, 300, fn ($e) => $e instanceof ConnectionException)
                ->post('/v1/western/natal/chart', $body)
                ->throw()
                ->json('data');
        });

        return view('charts.natal', ['chart' => $chart]);
    }
}
  • The time rule mirrors the API's own pattern, so an HTML <input type="time"> value without seconds (17:25) is accepted on both sides.
  • retry(...) only retries connection failures. A 400 will fail identically on every attempt, and retrying a 429 after 300 ms just spends the next slot; read Retry-After instead.
  • rememberForever never caches a failure - throw() raises before the closure returns.

Rendering it in Blade

resources/views/charts/natal.blade.php:

blade
<h2>Ascendant {{ $chart['ascendant']['sign'] }} {{ number_format($chart['ascendant']['degree'], 2) }}°</h2>
<p>Midheaven {{ $chart['midheaven']['sign'] }} {{ number_format($chart['midheaven']['degree'], 2) }}°</p>
<table>
  @foreach ($chart['planets'] as $p)
    <tr>
      <th>{{ $p['planet'] }}@if ($p['retrograde']) (R)@endif</th>
      <td>{{ $p['sign'] }} {{ number_format($p['degree_in_sign'], 2) }}°</td>
      <td>House {{ $p['house'] }}</td>
    </tr>
  @endforeach
</table>

For the sample birth with house_system: "whole-sign" the response had ascendant Libra 13.24°, midheaven Cancer 12.88°, and the Sun at Taurus 4.16° in house 8. With the default Placidus houses the same Sun sits in house 7 - the planet does not move, the house boundaries do (house systems). The response also carries houses (cusps) and the outer planets, which a wheel drawing needs.

The mistakes that bite first

  • Passing validated form input straight through. $request->validate() checks that lat is numeric but returns it as the string the form sent. Http::post then encodes "lat": "19.076", and the API answers 400 VALIDATION_ERROR - "Invalid input: expected number, received string". Cast, as the controller does.
  • `whole_sign` vs `whole-sign`. Vedic responses report their house system as "whole_sign" in meta; the Western endpoints' house_system input takes "whole-sign". Sending the underscore form returned a 400 listing the five accepted values - validate against that list with Rule::in, as above.
  • `env()` outside config files. After php artisan config:cache, env('ASTRONODE_API_KEY') returns null at runtime and every call is a 401. Read the key through config(...) only.

Questions

Which AstroNode endpoints does the Laravel / PHP example call?
2 endpoints: Western Natal Chart (POST /v1/western/natal/chart); Western Natal Planets (POST /v1/western/natal/planets).
Was the Laravel / PHP code on this page actually run?
The PHP was not executed - there is no PHP runtime on the build machine. The request it builds (same URL, headers and JSON body) was sent with curl to the in-memory API; the chart values, the whole_sign 400 and the string-number 400 are from those runs.