geocoder-php/BazingaGeocoderBundle

How to configure this bundle with geocoder-php/geo-plugin-provider?

MilesYM opened this issue · 6 comments

Hello and thank you for this great bundle.

I am trying to localize IP Addresses using GeoCode (https://github.com/geocoder-php/geo-plugin-provider)

So far I have done the following:

    bazinga_geocoder:
        fake_ip:    167.71.142.245
        providers:
            geoPlugin:
                factory: Bazinga\GeocoderBundle\ProviderFactory\GeoPluginFactory
                aliases:
                    - my_geocoder

And then in my service:

<?php

namespace App\Services;

use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;

class Geocoder
{
    private $geoPlugin;

    public function __construct(Provider $geoPlugin)
    {
        $this->geoPlugin = $geoPlugin;
    }

    public function getLocation($ip) {
        return $this->geoPlugin
            ->geocodeQuery(GeocodeQuery::create($ip));
    }
}

That I call from my controller like so:

$result = $geocoder->getLocation($request->server->get('REMOTE_ADDR'));

But I'm receiving the following error message:

Cannot resolve argument $geocoder of "App\Controller\Web\LandingPage\IndexController::aboutus()": Cannot autowire service "App\Services\Geocoder": argument "$geoPlugin" of method "__construct()" references interface "Geocoder\Provider\Provider" but no such service exists. You should maybe alias this interface to one of these existing services: "Geocoder\ProviderAggregator", "bazinga_geocoder.provider.geoPlugin".

Any idea what I should do? How do you configure the geocoder-php/geo-plugin-provider with this bundle?

Thank you!

Hello,

Your constructor argument must be $geoPluginGeocoder.

Thank you it did the trick.

How should I know how to call my argument?

Thank you

This feature was implemented by me and you can read about this more here:

https://github.com/geocoder-php/BazingaGeocoderBundle/blob/master/Resources/doc/index.md#autowiring-providers

😉

Thanks you! Amazing support here :)

Yes I read this part of the docu earlier, but it only contains an example for google maps. I imagine you won't be giving examples for all of the providers and was wondering if there is a way to guess how you should call your argument in general for autowiring or this something specific to your bundle?

Thank you

The keys aren't restricted, so you can name your geocoder how you want and then use the needed factory. So for example you can name it in the configuration lovelyGeoPlugin and then your argument would be $lovelyGeoPluginGeocoder🙂Maybe this should be more clarified 🤔
This is just a rule to register the alias appending the Geocoder part for argument.

Makes sens thank you so much.