-
Notifications
You must be signed in to change notification settings - Fork 0
Installation
Rumen Damyanov edited this page Jul 31, 2025
·
1 revision
Complete installation guide with configuration options and requirements for the php-geolocation package.
- PHP: 8.3 or higher
- Composer: For package management
- Cloudflare: For production geolocation data (optional in development)
composer require rumenx/php-geolocation- Download the latest release from GitHub
- Extract to your project directory
- Include the autoloader:
require_once 'path/to/php-geolocation/src/Geolocation.php';
require_once 'path/to/php-geolocation/src/GeolocationSimulator.php';Verify the installation works:
<?php
require_once 'vendor/autoload.php';
use Rumenx\Geolocation\Geolocation;
$geo = new Geolocation();
echo "Package installed successfully!\n";
echo "Local development: " . ($geo->isLocalDevelopment() ? 'Yes' : 'No') . "\n";use Rumenx\Geolocation\Geolocation;
// Default configuration
$geo = new Geolocation();
// Custom server data
$geo = new Geolocation($_SERVER);
// With country-to-language mapping
$countryToLanguage = [
'US' => ['en'],
'CA' => ['en', 'fr'],
'DE' => ['de'],
'FR' => ['fr'],
'ES' => ['es'],
];
$geo = new Geolocation($_SERVER, $countryToLanguage);
// With custom language cookie name
$geo = new Geolocation($_SERVER, $countryToLanguage, 'my_lang_cookie');// Custom server array for testing
$customServer = [
'HTTP_CF_IPCOUNTRY' => 'DE',
'HTTP_CF_CONNECTING_IP' => '192.168.1.100',
'HTTP_ACCEPT_LANGUAGE' => 'de-DE,de;q=0.9,en;q=0.8',
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)...'
];
$geo = new Geolocation($customServer, $countryToLanguage, 'lang');For local development, no special setup is required. The package automatically detects local environments:
$geo = new Geolocation();
if ($geo->isLocalDevelopment()) {
echo "Running in development mode\n";
// Simulation features available
$geo = Geolocation::simulate('US');
}For production, you need Cloudflare configured. See the Cloudflare Setup Guide for detailed instructions.
Add to a service provider or use directly in controllers:
// In a controller
use Rumenx\Geolocation\Geolocation;
class HomeController extends Controller
{
public function index()
{
$geo = new Geolocation();
$country = $geo->getCountryCode();
return view('home', compact('country'));
}
}Register as a service or use directly in controllers:
// In a controller
use Rumenx\Geolocation\Geolocation;
use Symfony\Component\HttpFoundation\Response;
class HomeController extends AbstractController
{
public function index(): Response
{
$geo = new Geolocation();
$country = $geo->getCountryCode();
return $this->render('home.html.twig', [
'country' => $country
]);
}
}Include in any PHP application:
require_once 'vendor/autoload.php';
use Rumenx\Geolocation\Geolocation;
$geo = new Geolocation();
$info = $geo->getGeoInfo();
// Use the geolocation data
include 'templates/header.php';
echo "<h1>Welcome visitor from " . $info['country_code'] . "!</h1>";
include 'templates/footer.php';Create a test file to verify everything works:
<?php
// test-geolocation.php
require_once 'vendor/autoload.php';
use Rumenx\Geolocation\Geolocation;
echo "=== PHP Geolocation Test ===\n\n";
// Test 1: Basic initialization
$geo = new Geolocation();
echo "✓ Package loaded successfully\n";
// Test 2: Local development detection
$isLocal = $geo->isLocalDevelopment();
echo "✓ Local development: " . ($isLocal ? 'Yes' : 'No') . "\n";
// Test 3: Simulation (works in any environment)
$simGeo = Geolocation::simulate('DE');
$country = $simGeo->getCountryCode();
echo "✓ Simulation works: {$country}\n";
// Test 4: Get information
$info = $simGeo->getGeoInfo();
echo "✓ Data extraction works: " . count($info) . " fields\n";
echo "\n=== Test completed successfully! ===\n";Run the test:
php test-geolocation.php# Clear composer cache
composer clear-cache
# Update dependencies
composer update
# Reinstall package
composer remove rumenx/php-geolocation
composer require rumenx/php-geolocationCheck your PHP version:
php --versionThe package requires PHP 8.3+. Update if necessary.
Ensure the Composer autoloader is included:
// Make sure this path is correct
require_once __DIR__ . '/vendor/autoload.php';- 📖 Quick Start Guide - Get started in 5 minutes
- 🏗️ Basic Usage Examples - Learn core functionality
- ⚡ Framework Integration - Framework-specific guides
- 🔧 Configuration - Advanced configuration options
Next: Quick Start Guide to begin using the package.