Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/w3bdesign/statens-vegvesen-php

Fetches Statens Vegvesen API data with PHP and renders the data with Twig. Styling with Bootstrap 5.
https://github.com/w3bdesign/statens-vegvesen-php

composer oop php twig

Last synced: about 1 month ago
JSON representation

Fetches Statens Vegvesen API data with PHP and renders the data with Twig. Styling with Bootstrap 5.

Awesome Lists containing this project

README

        

# Display vehicle information from Statens Vegvesen

Screenshot

## Description

PHP port of https://github.com/w3bdesign/Statens-Vegvesen

Fetching vehicle information from the REST API on and displaying it.

It requires an API key that has to be set inside `.env` (rename .env.example to .env) that you can get from

## Features

- PHP with OOP
- Separation of HTML and PHP code
- Composer with class autoloader
- Error handling and type annotations
- Twig for rendering HTML
- Bootstrap 5
- Responsive design
- Input validation with HTML5
- PHPDoc comments

## Instructions

To implement this inside your code, you'll need to do the following:

1. Run `composer install`

1. Import the autoloader with `require_once "vendor/autoload.php";`

2. Include the class in your project by adding the `use` statement at the top of your file:

```php

use Vehicle\VehicleDataFetcher;
use Vehicle\VehicleDataRender;

```

3. Create a new instance of the `VehicleDataFetcher` class:

```php
$hasError = false;

if ($_SERVER["REQUEST_METHOD"] === "POST") {
try {
$vehicleDataFetcher = new VehicleDataFetcher();
// This should be fetched from an input text value with $_POST
$regNummer = "AX58167;
$vehicleData = $vehicleDataFetcher->getVehicleData($regNummer);
} catch (Exception $e) {
$hasError = true;
}
}
```

4.Render the data inside your code:

```php
if (isset($vehicleData)) {
$vehicleDataRender = new VehicleDataRender($vehicleData);
echo $vehicleDataRender->render();
}
```

5. Handle the returned data or error message:

```php
if ($hasError) {
echo "



" . $e->getMessage() . "

";
return;
}

```