Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/AndresLopezCorrales/Countries-WebPage/llms.txt

Use this file to discover all available pages before exploring further.

Country-Click is a lightweight, stateless PHP web application with no database, no sessions, and no caching layer. Every page load either shows the world map with no country selected, or re-fetches country data live from the REST Countries API based on POST input received from either the search bar or the SVG map click handler. Because there is no persistent state, the application is entirely self-contained in a small set of PHP files and a single JavaScript file.

Request lifecycle

1

User visits index.php

PHP immediately includes controller/ApiConsumer.php and instantiates an ApiConsumer object. The page then renders the navbar (with the search form), the inline SVG world map, and calls $consumer->menu() to render the country info panel. On a fresh load with no POST data, menu() exits early and nothing is displayed in the panel.
2

User clicks a country or submits the search form

Clicking a country on the SVG map triggers the JavaScript click handler in resources/js/getname.js, which dynamically creates a hidden form and POSTs the xlink:title attribute of the clicked element as the field titulo back to index.php. Submitting the search form POSTs the typed text as the field search (along with the submit button name) to the same index.php.
3

ApiConsumer reads the POST data

ApiConsumer::searchBar() checks for $_POST['submit'] and $_POST['search']. If the search bar was used, it returns the typed text. If the map was clicked, GetNameController::clickCountries() checks for $_POST['titulo'] and returns the country title from the SVG element.
4

ApiConsumer::getSearchBarInfo() builds the API URL and calls file_get_contents()

The country name is URL-encoded (spaces replaced with %20) and appended to the REST Countries API base URL: https://restcountries.com/v3.1/name/{name}. The request is made with file_get_contents() using a custom stream context that sets ignore_errors: true, ensuring HTTP error bodies (like 404 responses) are returned as readable strings rather than triggering a PHP warning.
5

The JSON response is decoded and passed to GetAttributesController methods

The raw response string is decoded with json_decode($response, true). If the result does not contain an error status, each field is extracted by a dedicated method on GetAttributesController: getCommonName(), getOfficialName(), getRegion(), getPopulation(), getCapital(), getLanguage(), getTimezone(), and getFlag(). The language array is then joined into a comma-separated string with implode().
6

ApiConsumer::menu() renders the country info HTML panel

All eight extracted values are assembled into the $allAttributes array and returned from getSearchBarInfo(). Back in menu(), each value is unpacked by index and embedded directly into an echo’d HTML string that displays the country name, official name, flag image, capital, population, languages, timezone, and a region continent image.
7

The full page re-renders with the map and the populated info panel

Because index.php is the single entry point and the entire page is rendered on each POST request, the SVG map and the newly-populated info panel are delivered together in one HTML response. There is no AJAX or partial-page update.

Component diagram

index.php
  └── ApiConsumer
        ├── searchBar()           ← reads $_POST['search']
        ├── getSearchBarInfo()    ← calls REST Countries API
        │     ├── GetNameController::clickCountries()  ← reads $_POST['titulo']
        │     └── GetAttributesController
        │           ├── getCommonName($data)
        │           ├── getOfficialName($data)
        │           ├── getRegion($data)
        │           ├── getPopulation($data)
        │           ├── getCapital($data)
        │           ├── getLanguage($data)
        │           ├── getTimezone($data)
        │           └── getFlag($data)
        └── menu()               ← renders HTML info panel

resources/js/getname.js
  └── click listener on .country elements
        └── creates hidden form, POSTs 'titulo' to index.php

Stateless design

Because there is no session, database, or cache, every request is entirely self-contained. The PHP process receives a POST (or a plain GET on fresh load), optionally fetches from the REST Countries API, renders the full HTML page, and terminates. Nothing persists between requests. This design makes deployment trivial — the application runs on any standard PHP web host with no configuration beyond uploading the files. There are no migrations, no environment-specific setup, and no background workers. The trade-off is that every search or map click results in a live outbound HTTP request to restcountries.com. If the API is unavailable or slow, the user will experience a delay or an error message. Responses are never cached, so repeated lookups for the same country always incur a fresh API call.

Input priority

Within getSearchBarInfo(), the search bar takes priority over the map click. If searchBar() returns a non-empty string, that value is used as the country name. Only when searchBar() returns an empty string does the method fall back to GetNameController::clickCountries() to read the map click value. This ensures that if a user somehow triggers both inputs simultaneously, the typed search text always wins.
ApiConsumer.php
$nameCountry = $this->searchBar();
if ($nameCountry == "") {
    $nameCountry = $getNameClick->clickCountries();
} else {
    $nameCountry = $this->searchBar();
}
The app uses PHP’s file_get_contents() with stream_context_create(['http' => ['ignore_errors' => true]]) so HTTP error responses (like 404) are returned as body text rather than triggering a PHP warning. This allows the application to read the JSON error payload from the REST Countries API and display a user-friendly message instead of a fatal PHP error.

Build docs developers (and LLMs) love