I’m in Laravel v10 and I wanted to show the ip address of client with geo location in details.
So I installed the package torann/geoip:
composer require torann/geoip
Then:
php artisan vendor:publish --provider="TorannGeoIPGeoIPServiceProvider" --tag=config
And after that created a middleware named GeoIPMiddleware which goes here:
<?php
namespace AppHttpMiddleware;
use Closure;
use IlluminateHttpRequest;
use TorannGeoIPGeoIP;
class GeoIPMiddleware
{
public function handle(Request $request, Closure $next)
{
$config = config('geoip');
$geoip = new GeoIP($config, $request);
$request->attributes->add([
'geoip' => $geoip->getLocation(), // Add location data to the request attributes
]);
return $next($request);
}
}
And also registered it at kernel.php:
protected $middleware = [
// AppHttpMiddlewareTrustHosts::class,
AppHttpMiddlewareTrustProxies::class,
AppHttpMiddlewareGeoIPMiddleware::class,
...
];
And here is the route:
Route::get('/ip/{ip}', [IPDetailsController::class, 'showIPDetails']);
But when I test it like this, I get an error:
http://localhost:8000/ip/32.2.185.118
Error:
TorannGeoIPGeoIP::__construct(): Argument #2 ($cache) must be of type IlluminateCacheCacheManager, IlluminateHttpRequest given, called in D:rootappHttpMiddlewareGeoIPMiddleware.php on line 15
According to the line of the middleware:
$geoip = new GeoIP($config, $request);
So what’s going wrong here? How can I solve this issue?
Note that I have also registered at ipapi website and created a key for myself and placed that in .env file like this: IPAPI_KEY=MY_KEY
And this is also my config/geoip.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Logging Configuration
|--------------------------------------------------------------------------
|
| Here you may configure the log settings for when a location is not found
| for the IP provided.
|
*/
'log_failures' => true,
/*
|--------------------------------------------------------------------------
| Include Currency in Results
|--------------------------------------------------------------------------
|
| When enabled the system will do it's best in deciding the user's currency
| by matching their ISO code to a preset list of currencies.
|
*/
'include_currency' => true,
/*
|--------------------------------------------------------------------------
| Default Service
|--------------------------------------------------------------------------
|
| Here you may specify the default storage driver that should be used
| by the framework.
|
| Supported: "maxmind_database", "maxmind_api", "ipapi"
|
*/
'service' => 'ipapi',
/*
|--------------------------------------------------------------------------
| Storage Specific Configuration
|--------------------------------------------------------------------------
|
| Here you may configure as many storage drivers as you wish.
|
*/
'services' => [
'maxmind_database' => [
'class' => TorannGeoIPServicesMaxMindDatabase::class,
'database_path' => storage_path('app/geoip.mmdb'),
'update_url' => sprintf('https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-City&license_key=%s&suffix=tar.gz', env('MAXMIND_LICENSE_KEY')),
'locales' => ['en'],
],
'maxmind_api' => [
'class' => TorannGeoIPServicesMaxMindWebService::class,
'user_id' => env('MAXMIND_USER_ID'),
'license_key' => env('MAXMIND_LICENSE_KEY'),
'locales' => ['en'],
],
'ipapi' => [
'class' => TorannGeoIPServicesIPApi::class,
'secure' => true,
'key' => env('IPAPI_KEY'),
'continent_path' => storage_path('app/continents.json'),
'lang' => 'en',
],
'ipgeolocation' => [
'class' => TorannGeoIPServicesIPGeoLocation::class,
'secure' => true,
'key' => env('IPGEOLOCATION_KEY'),
'continent_path' => storage_path('app/continents.json'),
'lang' => 'en',
],
'ipdata' => [
'class' => TorannGeoIPServicesIPData::class,
'key' => env('IPDATA_API_KEY'),
'secure' => true,
],
'ipfinder' => [
'class' => TorannGeoIPServicesIPFinder::class,
'key' => env('IPFINDER_API_KEY'),
'secure' => true,
'locales' => ['en'],
],
],
/*
|--------------------------------------------------------------------------
| Default Cache Driver
|--------------------------------------------------------------------------
|
| Here you may specify the type of caching that should be used
| by the package.
|
| Options:
|
| all - All location are cached
| some - Cache only the requesting user
| none - Disable cached
|
*/
'cache' => 'all',
/*
|--------------------------------------------------------------------------
| Cache Tags
|--------------------------------------------------------------------------
|
| Cache tags are not supported when using the file or database cache
| drivers in Laravel. This is done so that only locations can be cleared.
|
*/
'cache_tags' => ['torann-geoip-location'],
/*
|--------------------------------------------------------------------------
| Cache Expiration
|--------------------------------------------------------------------------
|
| Define how long cached location are valid.
|
*/
'cache_expires' => 30,
/*
|--------------------------------------------------------------------------
| Default Location
|--------------------------------------------------------------------------
|
| Return when a location is not found.
|
*/
'default_location' => [
'ip' => '127.0.0.0',
'iso_code' => 'US',
'country' => 'United States',
'city' => 'New Haven',
'state' => 'CT',
'state_name' => 'Connecticut',
'postal_code' => '06510',
'lat' => 41.31,
'lon' => -72.92,
'timezone' => 'America/New_York',
'continent' => 'NA',
'default' => true,
'currency' => 'USD',
],
];