You are here

public function Ip2CountryController::lookupAction in IP-based Determination of a Visitor's Country 8

AJAX callback to lookup an IP address in the database.

Parameters

string $ip_address: String with IP address.

Return value

string JSON object for display by jQuery script.

1 string reference to 'Ip2CountryController::lookupAction'
ip2country.routing.yml in ./ip2country.routing.yml
ip2country.routing.yml

File

src/Controller/Ip2CountryController.php, line 137

Class

Ip2CountryController
Controller routines for user routes.

Namespace

Drupal\ip2country\Controller

Code

public function lookupAction($ip_address) {

  // Return results of manual lookup.
  $country_code = $this->ip2countryLookup
    ->getCountry($ip_address);
  if ($country_code) {
    $country_list = $this->countryManager
      ->getList();
    $country_name = $country_list[$country_code];
    print Json::encode([
      'message' => $this
        ->t('IP Address @ip is assigned to @country (@code).', [
        '@ip' => $ip_address,
        '@country' => $country_name,
        '@code' => $country_code,
      ]),
    ]);
  }
  else {
    print Json::encode([
      'message' => $this
        ->t('IP Address @ip is not assigned to a country.', [
        '@ip' => $ip_address,
      ]),
    ]);
  }
  exit;
}