15
DecLaravel GeoIP is a package introduced by Daniel Stainback. With the help of this package, you can easily determine your user’s general location and currency by simply looking up their IP address. Knowing this can better help you in knowing what to show them and in general making their experience on your website more enjoyable and easier for us.
You can install this package via composer, Just run this command:
composer require torann/geoip
Once installed you need to register the service provider with the application. Open up config/app.php
and find the providers key.
'providers' => [
\Torann\GeoIP\GeoIPServiceProvider::class,
]
This package also comes with an optional facade, which provides an easy way to call the class. Open up config/app.php
and find the aliases key.
'aliases' => [
'GeoIP' => \Torann\GeoIP\Facades\GeoIP::class,
];
Publish the configurations
Run this on the command line from the root of your project:
php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config
A configuration file will be published to config/geoip.php
.
The simplest way to use this method is through the helper function geoip()
or by using the facade \Torann\GeoIP\Facades\GeoIP
. For the examples below we will use the helper method.
Get the location data for a website visitor:
geoip($ip = null);
Arguments:
$ip
- The Ip to look up. If not set the application default to the remote address.
Example Location Object
\Torann\GeoIP\Location {
#attributes:array [
'ip' => '232.223.11.11',
'iso_code' => 'US',
'country' => 'United States',
'city' => 'New Haven',
'state' => 'CT',
'state_name' => 'Connecticut',
'postal_code' => '06510',
'lat' => 41.28,
'lon' => -72.88,
'timezone' => 'America/New_York',
'continent' => 'NA',
'currency' => 'USD',
'default' => false,
]
}
Default Location
In the case that a location is not found the fallback location will be returned with the default
parameter set to true
. To set your own default change it in the configurations config/geoip.php
.
Updating Service Data
Some services may need to update local files. For example, the MaxMind Database service fetches a remote database and saves it to the local file system.
php artisan geoip:update
Clearing Cached Locations
Some cache drivers offer the ability to clear stored locations.
php artisan geoip:clear
When the geoip()
helper function is used without arguments it will return the \Torann\GeoIP\GeoIP
instance, and with this, we can do all types of amazing things.
getLocation($ip = null)
Get the location from the provided IP.
Arguments:
$ip
- The Ip to look up. If not set the application default to the remote address.
geoip()->getLocation('27.974.399.65');
getService()
Will return the default service used for determining location.
getClientIP()
Will return the user’s IP address.
You can create your Custom service or use the services available in this package, For more details, You can visit its documentation & Source code on Github.
Published at : 15-12-2021
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project