An open API service indexing awesome lists of open source software.

https://github.com/geocoder-php/geocoderlaravel

Geocoder service provider for Laravel
https://github.com/geocoder-php/geocoderlaravel

geocoding laravel-package

Last synced: about 2 months ago
JSON representation

Geocoder service provider for Laravel

Awesome Lists containing this project

README

          

[![Travis](https://img.shields.io/travis/geocoder-php/GeocoderLaravel.svg)](https://travis-ci.org/geocoder-php/GeocoderLaravel)
[![Scrutinizer](https://img.shields.io/scrutinizer/g/geocoder-php/GeocoderLaravel.svg)](https://scrutinizer-ci.com/g/geocoder-php/GeocoderLaravel/)
[![Coveralls](https://img.shields.io/coveralls/geocoder-php/GeocoderLaravel.svg)](https://coveralls.io/github/geocoder-php/GeocoderLaravel)
[![GitHub release](https://img.shields.io/github/release/geocoder-php/GeocoderLaravel.svg)](https://github.com/geocoder-php/GeocoderLaravel/releases)
[![Packagist](https://img.shields.io/packagist/dt/toin0u/geocoder-laravel.svg)](https://packagist.org/packages/toin0u/geocoder-laravel)

# Geocoder for Laravel

> If you still use **Laravel 4**, please check out the `0.4.x` branch
[here](https://github.com/geocoder-php/GeocoderLaravel/tree/0.4.x).

**Version 13.0.0 is a backwards-compatibility-breaking update. Please review
the _Upgrading_ section, especially the new default HTTP adapter, before
installing.**

> **Versioning change:** starting with `13.0.0`, this package's major version
> tracks the highest supported Laravel major version, instead of the upstream
> Geocoder PHP version. So `13.x` supports Laravel 11/12/13, the next major
> will be `14.x` when Laravel 14 ships, and so on. The previous tag was
> `5.0.0`; the jump to `13.0.0` reflects the new versioning scheme, not 9
> intermediate releases.

This package allows you to use [**Geocoder**](http://geocoder-php.org/Geocoder/)
in [**Laravel**](http://laravel.com/).

## Requirements
- PHP >= 8.2
- Laravel >= 11.0

## Installation
1. Install the package via composer:
```sh
composer require toin0u/geocoder-laravel
```

2. **If you are running Laravel 5.5 (the package will be auto-discovered), skip
this step.** Find the `providers` array key in `config/app.php` and register
the **Geocoder Service Provider**:
```php
// 'providers' => [
Geocoder\Laravel\Providers\GeocoderService::class,
// ];
```
3. **Optional** I recommend adding the following lines to your `composer.json` file to prevent stale caches when upgrading or updating the package, both in your live and dev environments:
```json
"post-update-cmd": [
"@php artisan cache:clear",
],
"post-install-cmd": [
"@php artisan cache:clear",
]
```

## Configuration
Pay special attention to the language and region values if you are using them.
For example, the GoogleMaps provider uses TLDs for region values, and the
following for language values: https://developers.google.com/maps/faq#languagesupport.

Further, a special note on the GoogleMaps provider: if you are using an API key,
you must also use set HTTPS to true. (Best is to leave it true always, unless
there is a special requirement not to.)

See the [Geocoder documentation](http://geocoder-php.org/Geocoder/) for a list
of available adapters and providers.

### Dedicated Cache Store *Recommended*
To implement the dedicated cache store, add another redis store entry in
`config/database.php`, something like the following:
```php
"redis" => [
// ...

"geocode-cache" => [ // choose an appropriate name
'host' => env('REDIS_HOST', '192.168.10.10'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 1, // be sure this number differs from your other redis databases
],
]
```

You will also need to add an entry in `config/cache.php` to point to this redis
database:
```php
"stores" => [
// ...

"geocode" => [
'driver' => 'redis',
'connection' => 'geocode-cache',
],
],
```

Finally, configure Geocoder for Laravel to use this store. Edit
`config/geocoder.php`:
```php
"cache" => [
"store" => "geocode",

// ...
],
```

#### Disabling Caching on a Query-Basis
You can disable caching on a query-by-query basis as needed, like so:
```php
$results = app("geocoder")
->doNotCache()
->geocode('Los Angeles, CA')
->get();
```

#### ⚠️ Laravel 13 `cache.serializable_classes` — Important

> Laravel 13 introduced [`cache.serializable_classes`](https://laravel.com/docs/13.x/upgrade#cache-serializable_classes-configuration) as a security hardening measure, defaulting to `false` to block deserialization of arbitrary PHP objects from the cache. This package stores `Collection`s of `Address` objects in the cache, which would silently break caching under the new default.

To keep caching working out of the box without forcing you to maintain an
allow-list as new providers are installed, **this package scans the installed
Geocoder vendor directories at boot and merges every model class it finds into
your application's `cache.serializable_classes` allow-list**. Whatever
providers you have installed under `vendor/geocoder-php/*` are covered
automatically — there's no curated list to go stale.

**🔐 Security implication:** the package narrowly relaxes Laravel 13's hardening
for the geocoder model classes installed in your `vendor/` directory. Other
PHP objects you store in the cache remain blocked unless you explicitly allow
them. The blast radius is bounded to classes you've already deliberately
installed via composer.

**Opting out.** Set `auto_register_serializable_classes` to `false` in your
`config/geocoder.php`:

```php
'cache' => [
// ...

'auto_register_serializable_classes' => false,
],
```

When opted out, the package will not touch `cache.serializable_classes` at all.
You then have two reasonable paths:

1. **Manage the allow-list yourself.** Add the geocoder model classes to
`config/cache.php`'s `serializable_classes` directly. Caching keeps working
under your explicit control. Pick this if you want to audit exactly which
PHP objects your application allows to deserialize from cache.

2. **Disable caching for geocoder queries.** Call
`app('geocoder')->doNotCache()` on each query, or set `cache.duration` to
`0` in `config/geocoder.php`. Pick this if you don't want to maintain the
allow-list and can absorb the per-request API cost.

Doing neither under Laravel 13 could cause `__PHP_Incomplete_Class` corruption
on cached results.

### Providers
If you are upgrading and have previously published the geocoder config file, you
need to add the `cache-duration` variable, otherwise cache will be disabled
(it will default to a `0` cache duration). The default cache duration provided
by the config file is `999999999` seconds, essentially forever.

By default, the configuration specifies a Chain provider, containing the
GoogleMaps provider for addresses as well as reverse lookups with lat/long,
and the GeoIP provider for IP addresses. The first to return a result
will be returned, and subsequent providers will not be executed. The default
config file is kept lean with only those two providers.

However, you are free to add or remove providers as needed, both inside the
Chain provider, as well as along-side it. The following is the default
configuration provided by the package:
```php
use Geocoder\Laravel\Http\LaravelHttpClient;
use Geocoder\Provider\Chain\Chain;
use Geocoder\Provider\GeoPlugin\GeoPlugin;
use Geocoder\Provider\GoogleMaps\GoogleMaps;

return [

/*
|--------------------------------------------------------------------------
| Cache Duration
|--------------------------------------------------------------------------
|
| Specify the cache duration in seconds. The default approximates a forever
| cache, but there are certain issues with Laravel's forever caching
| methods that prevent us from using them in this project.
|
| Default: 9999999 (integer)
|
*/
'cache-duration' => 9999999,

/*
|--------------------------------------------------------------------------
| Providers
|--------------------------------------------------------------------------
|
| Here you may specify any number of providers that should be used to
| perform geocaching operations. The `chain` provider is special,
| in that it can contain multiple providers that will be run in
| the sequence listed, should the previous provider fail. By
| default the first provider listed will be used, but you
| can explicitly call subsequently listed providers by
| alias: `app('geocoder')->using('google_maps')`.
|
| Please consult the official Geocoder documentation for more info.
| https://github.com/geocoder-php/Geocoder#providers
|
*/
'providers' => [
Chain::class => [
GoogleMaps::class => [
env('GOOGLE_MAPS_LOCALE', 'us'),
env('GOOGLE_MAPS_API_KEY'),
],
GeoPlugin::class => [],
],
],

/*
|--------------------------------------------------------------------------
| Adapter
|--------------------------------------------------------------------------
|
| The HTTP adapter to use when communicating with geocoding services. By
| default this package ships a PSR-18 client that delegates to Laravel's
| `Http` facade — this gives you `Http::fake()` in tests, native retry
| and timeout configuration, and any HTTP middleware you've registered.
|
| Provide any class that implements `Psr\Http\Client\ClientInterface` to
| swap in a different adapter (e.g., `Http\Client\Curl\Client` from
| `php-http/curl-client`, which you would need to install separately).
|
| To pass constructor arguments (timeouts, proxies, client options, etc.)
| use the array form `[Class => [args]]`. Arguments are forwarded to the
| adapter's constructor — it's on you to match its signature. Named args
| are supported: `[Class => ['timeout' => 10]]`.
|
| Default: LaravelHttpClient::class
|
| Examples:
| 'adapter' => LaravelHttpClient::class,
| 'adapter' => [LaravelHttpClient::class => ['timeout' => 10, 'retry' => [3, 100]]],
| 'adapter' => [Http\Client\Curl\Client::class => [null, null, [CURLOPT_PROXY => '...']]],
|
*/
'adapter' => LaravelHttpClient::class,

/*
|--------------------------------------------------------------------------
| Reader
|--------------------------------------------------------------------------
|
| You can specify a reader for specific providers, like GeoIp2, which
| connect to a local file-database. The reader should be set to an
| instance of the required reader class or an array containing the reader
| class and arguments.
|
| Please consult the official Geocoder documentation for more info.
| https://github.com/geocoder-php/geoip2-provider
|
| Default: null
|
*/
'reader' => null,

];
```

### Adapters
By default we ship `Geocoder\Laravel\Http\LaravelHttpClient`, a thin PSR-18
client that delegates every request to Laravel's `Http` facade. This means:

- `Http::fake()` and `Http::assertSent()` work in your tests with no extra setup
- One less third-party HTTP client to manage

#### Configuring Adapter Options
You can pass constructor arguments to the adapter via the array form
`[Class => [args]]` in `config/geocoder.php`. Arguments are forwarded directly
to the adapter's constructor — it's on you to match its signature. Named
arguments are supported.

The default `LaravelHttpClient` accepts `timeout`, `connectTimeout`, `retry`,
and `options` (Guzzle transport options). These are forwarded to the
underlying Laravel `PendingRequest`:
```php
'adapter' => [LaravelHttpClient::class => [
'timeout' => 10,
'connectTimeout' => 3,
'retry' => [3, 100], // [times, sleepMilliseconds]
'options' => ['verify' => false], // Guzzle transport options
]],
```

#### Using a Different Transport
Set `'adapter'` to any class that implements `Psr\Http\Client\ClientInterface`.
To go back to the previous CURL adapter, install `php-http/curl-client` and
pass your CURL options the same way:
```php
'adapter' => [Http\Client\Curl\Client::class => [
null,
null,
[CURLOPT_PROXY => env('CURL_PROXY'), CURLOPT_PROXYUSERPWD => env('CURL_PROXYUSERPWD')],
]],
```

### Customization
If you would like to make changes to the default configuration, publish and
edit the configuration file:
```sh
php artisan vendor:publish --provider="Geocoder\Laravel\Providers\GeocoderService" --tag="config"
```

## Usage
The service provider initializes the `geocoder` service, accessible via the
facade `Geocoder::...` or the application helper `app('geocoder')->...`.

### Geocoding Addresses
#### Get Collection of Addresses
```php
app('geocoder')->geocode('Los Angeles, CA')->get();
```

#### Get IP Address Information
```php
app('geocoder')->geocode('8.8.8.8')->get();
```

#### Reverse-Geocoding
```php
app('geocoder')->reverse(43.882587,-103.454067)->get();
```

#### Localizing Results
Use the `locale()` method to request localized formatting from providers that
support it (GoogleMaps, Nominatim, etc.). Results are cached separately per
locale, so switching locales doesn't pollute each other's cache entries:
```php
app('geocoder')->locale('it')->geocode('Bologna')->get();
app('geocoder')->locale('fr')->reverse(45.5, -73.5)->get();
```

If you need to build a query with additional options, you can also set the
locale directly on a query and pass it through `geocodeQuery()` or
`reverseQuery()`:
```php
use Geocoder\Query\GeocodeQuery;

$query = GeocodeQuery::create('Bologna')->withLocale('it');
app('geocoder')->geocodeQuery($query)->get();
```

#### Dumping Results
```php
app('geocoder')->geocode('Los Angeles, CA')->dump('kml');
```

#### Dependency Injection
```php
use Geocoder\Laravel\ProviderAndDumperAggregator as Geocoder;

class GeocoderController extends Controller
{
public function getGeocode(Geocoder $geocoder)
{
$geocoder->geocode('Los Angeles, CA')->get()
}
}
```

## Upgrading
Anytime you upgrade this package, please remember to clear your cache, to prevent incompatible cached responses when breaking changes are introduced (this should hopefully only be necessary in major versions):
```sh
php artisan cache:clear
```

### 5.x to 13.x
Update your `composer.json`:
```json
"toin0u/geocoder-laravel": "^13.0",
```

> Yes, the jump from `5.x` to `13.x` is intentional — see the versioning note
> at the top of this README. There are no `5.x` through `12.x` releases.

**Breaking: default HTTP adapter changed.** The default `'adapter'` in
`config/geocoder.php` is now `Geocoder\Laravel\Http\LaravelHttpClient`
instead of `Http\Client\Curl\Client`. The new adapter is a PSR-18 client
that delegates to Laravel's `Http` facade, so `Http::fake()`, retries,
timeouts, and middleware all work transparently.

If you have **published the geocoder config previously**, your config file
still pins the old curl adapter and will fail at runtime once
`php-http/curl-client` is no longer installed. Pick one:

- **Recommended:** delete (or rename) your published `config/geocoder.php`
and re-publish it, then re-apply your customizations.
- Or edit the existing file: change `use Http\Client\Curl\Client;` to
`use Geocoder\Laravel\Http\LaravelHttpClient;` and change
`'adapter' => Client::class` to `'adapter' => LaravelHttpClient::class`.
- Or, if you want to keep the curl adapter, install it explicitly:
`composer require php-http/curl-client`. The published config keeps working
unchanged.

**Other notable changes in 13.x:**
- Minimum PHP raised to 8.2; minimum Laravel raised to 11.x.
- `php-http/curl-client` removed from required dependencies (install it
yourself if you still need it).
- `MaxMindBinary` provider support removed (the underlying PHP package was
abandoned). Use `geocoder-php/geoip2-provider` for MaxMind data instead.

### 4.x to 5.x
Update your `composer.json`:
```json
"toin0u/geocoder-laravel": "^5.0",
```

### 1.x to 4.x
Update your composer.json file:
```json
"toin0u/geocoder-laravel": "^4.0",
```

The one change to keep in mind here is that the results returned from
`Geocoder for Laravel` are now using the Laravel-native Collections class
instead of returning an instance of `AddressCollection`. This should provide
greater versatility in manipulation of the results, and be inline with
expectations for working with Laravel. The existing `AddressCollection`
methods should map straight over to Laravel's `Collection` methods. But be sure
to double-check your results, if you have been using `count()`,
`first()`, `isEmpty()`, `slice()`, `has()`, `get()`, or `all()` on your results.

Also, `getProviders()` now returns a Laravel Collection instead of an array.

**Alert:** if you have been using the `getIterator()` method, it is no longer
needed. Simply iterate over your results as you would any other Laravel
collection.

**Deprecated:**
- the `all()` method on the geocoder is being deprecated in favor of using
`get()`, which will return a Laravel Collection. You can then run `all()`
on that. This method will be removed in version 5.0.0.
- the `getProvider()` method on the geocoder is being deprecated in favor of using
`getProviders()`, which will return a Laravel Collection. You can then run `first()`
on that to get the same result. This method will be removed in version 5.0.0.

**Added:** this version introduces a new way to create more complex queries:
- geocodeQuery()
- reverseQuery()

Please see the [Geocoder documentation](https://github.com/geocoder-php/Geocoder)
for more details.

### 0.x to 1.x
If you are upgrading from a pre-1.x version of this package, please keep the
following things in mind:

1. Update your composer.json file as follows:

```json
"toin0u/geocoder-laravel": "^1.0",
```

2. Remove your `config/geocoder.php` configuration file. (If you need to customize it, follow the configuration instructions below.)
3. Remove any Geocoder alias in the aliases section of your `config/app.php`. (This package auto-registers the aliases.)
4. Update the service provider entry in your `config/app.php` to read:

```php
Geocoder\Laravel\Providers\GeocoderService::class,
```

5. If you are using the facade in your code, you have two options:
1. Replace the facades `Geocoder::` (and remove the corresponding `use` statements) with `app('geocoder')->`.
2. Update the `use` statements to the following:

```php
use Geocoder\Laravel\Facades\Geocoder;
```

6. Update your query statements to use `->get()` (to retrieve a collection of
GeoCoder objects) or `->all()` (to retrieve an array of arrays), then iterate
to process each result.

## Troubleshooting
- Clear cache: `php artisan cache:clear`.
- If you are still experiencing difficulties, please please open an issue on GitHub:
https://github.com/geocoder-php/GeocoderLaravel/issues.

## Changelog
https://github.com/geocoder-php/GeocoderLaravel/blob/master/CHANGELOG.md

## Contributor Code of Conduct
Please note that this project is released with a
[Contributor Code of Conduct](https://github.com/geocoder-php/Geocoder#contributor-code-of-conduct).
By participating in this project you agree to abide by its terms.

## License
GeocoderLaravel is released under the MIT License. See the bundled
[LICENSE](https://github.com/geocoder-php/GeocoderLaravel/blob/master/LICENSE)
file for details.