## Getting Started
### Minimal Setup
1. **Installation**:
```bash
composer require azine/geoblocking-bundle
Register the bundle in config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 2/3):
Azine\GeoBlockingBundle\AzineGeoBlockingBundle::class => ['all' => true],
Basic Configuration:
Add minimal config to config/packages/azine_geo_blocking.yaml (Symfony 4+) or app/config/config.yml (Symfony 2/3):
azine_geo_blocking:
enabled: true
countries:
blacklist: ["US", "CN"] # Block users from these countries
routes:
whitelist: ["fos_user_security_login"] # Allow login route
First Use Case:
/dashboard) from a blocked country (e.g., US).accessDenied.html.twig template (located in vendor/azine/geoblocking-bundle/Resources/views/) is rendered.Request Handling:
The bundle listens to the kernel.request event. For each request:
lookup_adapter.block_anonymouse_users_only: true).Integration with FOSUserBundle:
routes:
whitelist:
- fos_user_security_login
- fos_user_security_login_check
- fos_user_security_logout
Dynamic Route Handling:
routes:
blacklist:
- app_homepage # Route defined in `routing.yml`
accessDenied view by specifying a custom Twig template:
access_denied_view: "AcmeBundle:Page:geoBlocked.html.twig"
Cookie-Based Exceptions:
// Controller
$response->headers->setCookie(new Cookie("geoblocking_allow_cookie", true, new \DateTime("+2 days")));
allow_by_cookie: true
IP Whitelisting:
ip_whitelist:
- "123.45.67.89"
- "/^192\.168\.\d+\.\d+$/" # Regex for private networks
Search Bot Allowance:
allow_search_bots: true
search_bot_domains:
- ".googlebot.com"
GeoIP Module Dependency:
geoip_country_code_by_name() not found.pecl install geoip) or use the MaxmindGeoIPBundle:
lookup_adapter: azine_geo_blocking.maxmind.lookup.adapter
Requires maxmind/geoip and its bundle configuration.Route Name Mismatches:
routes: config match those in routing.yml (use debug:router to list routes).Logged-In User Bypass:
block_anonymouse_users_only: true (default) and that the user session is properly authenticated (e.g., FOSUserBundle).Private IP Handling:
allow_private_ips: true (default) or whitelist specific IPs.Cookie Persistence:
geoblocking_allow_cookie expires too soon.new \DateTime("+7 days")).Case Sensitivity in Country Codes:
"us" vs "US" cause inconsistencies."US", "CN") in config.Log Blocked Requests: Enable logging to debug blocked requests:
logBlockedRequests: true
Check Symfony logs (var/log/dev.log) for entries like:
[GeoBlocking] Blocked request from IP [X.X.X.X] (Country: US) on route [app_homepage].
Test with Known IPs: Use services like IP2Location to test with specific country IPs:
curl -H "X-Forwarded-For: 66.249.64.0" http://your-site.com/dashboard
Override Default Views:
Copy accessDenied.html.twig to your project (e.g., templates/AzineGeoBlocking/accessDenied.html.twig) to customize without modifying the bundle.
Custom Lookup Adapter:
Implement GeoIpLookupAdapterInterface for alternative providers (e.g., AWS Location Service):
// src/Service/CustomGeoIpAdapter.php
class CustomGeoIpAdapter implements GeoIpLookupAdapterInterface {
public function getCountryCodeByIp($ip) {
// Your logic here (e.g., API call)
return "US";
}
}
Register as a service:
services:
azine_geo_blocking.custom.adapter:
class: App\Service\CustomGeoIpAdapter
tags: ['azine_geo_blocking.lookup_adapter']
Update config:
lookup_adapter: azine_geo_blocking.custom.adapter
GeoIP Lookup Overhead:
$cache = $this->container->get('cache.app');
$countryCode = $cache->get($ip, function() use ($ip) {
return $this->geoIpAdapter->getCountryCodeByIp($ip);
});
Route Matching:
$router = $this->container->get('router');
$routeName = $router->getRouteCollection()->getNameForPath($request->getPathInfo());
Event Listeners:
Extend functionality by subscribing to azine.geo_blocking.block_request:
// src/EventListener/CustomGeoBlockListener.php
class CustomGeoBlockListener {
public function onBlockRequest(GeoBlockEvent $event) {
if ($event->isBlocked()) {
// Custom logic (e.g., redirect to a survey)
$event->setResponse(new RedirectResponse('/survey'));
}
}
}
Register the listener:
services:
App\EventListener\CustomGeoBlockListener:
tags:
- { name: kernel.event_listener, event: azine.geo_blocking.block_request, method: onBlockRequest }
Dynamic Country Lists: Fetch country whitelists/blacklists from a database or API:
// Override the bundle's CountryValidator
class DynamicCountryValidator extends CountryValidator {
public function isAllowed($countryCode) {
$allowedCountries = $this->fetchFromDatabase(); // Your logic
return in_array(strtoupper($countryCode), $allowedCountries);
}
}
Replace the default validator in services:
services:
azine_geo_blocking.country_validator:
class: App\Validator\DynamicCountryValidator
arguments: ['@azine_geo_blocking.country_list']
Multi-Tenant Support: Store geoblocking rules per tenant:
# Per-tenant config (e.g., in a database)
azine_geo_blocking:
countries:
blacklist: "%tenant_blacklist%" # Resolved via parameter bag
Use Symfony’s parameter bag to inject
How can I help you explore Laravel packages today?