megakit/laravel-location

This library helps to quickly locate a user's location.

v0.2.0 2018-09-17 14:46 UTC

This package is auto-updated.

Last update: 2024-04-18 03:08:01 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

This library helps you quickly locate a user location.

Installation

$ composer require megakit/laravel-location

Configuration

$ php artisan vendor:publish --provider="MegaKit\Laravel\Location\LocationServiceProvider"

Default config file

<?php

use MegaKit\Laravel\Location\Resolvers\DefaultLocationResolver;
use MegaKit\Laravel\Location\Resolvers\NullSubdomainLocationResolver;
use MegaKit\Laravel\Location\Transformers\CountryNameTransformer;

return [

    'source' => env('LOCATION_SOURCE', 'chain'),

    'transformer' => CountryNameTransformer::class,

    'sources' => [
        'chain' => [
            'driver' => 'chain',
            'sources' => ['subdomain', 'cookie', 'geo', 'default'],
        ],
        'subdomain' => [
            'driver' => 'subdomain',
            'resolver' => NullSubdomainLocationResolver::class,
            'url' => env('APP_URL'),
        ],
        'cookie' => [
            'driver' => 'cookie',
            'name' => 'laravel_location',
        ],
        'geo' => [
            'driver' => 'geo',
            'provider' => env('LOCATION_GEO_PROVIDER', 'chain'),
            'cache' => true,
            'cache_driver' => env('LOCATION_GEO_CACHE_DRIVER'),
            'cache_lifetime' => 0,
        ],
        'default' => [
            'driver' => 'resolver',
            'resolver' => DefaultLocationResolver::class,
        ],
    ],

    'geo' => [
        'providers' => [
            'chain' => [
                'providers' => ['geocoder-php'],
            ],
            'geocoder-php' => [
                //
            ],
        ],
    ],

];

Usage

You have a few options for use it.

<?php

namespace App\Http\Controllers;

use Illuminate\Contracts\Container\Container;
use Illuminate\Http\Request;
use MegaKit\Laravel\Location\Contracts\LocationLocator;
use MegaKit\Laravel\Location\Contracts\LocationResolver;
use MegaKit\Laravel\Location\Models\Location;

class HomeController extends Controller
{
    /**
     * @param Request $request
     * @param LocationResolver $locationResolver
     * @return string
     */
    public function index(Request $request, LocationResolver $locationResolver)
    {
        return $locationResolver->resolve($request)->getCountry()->getName();
    }

    /**
     * @param Location $location
     * @return string
     */
    public function dependencyInjection(Location $location)
    {
        return $location->getCountry()->getName();
    }

    /**
     * @param Request $request
     * @return string
     */
    public function request(Request $request)
    {
        return $request->location()->getCountry()->getName();
    }

    /**
     * @param Container $container
     * @return string
     */
    public function container(Container $container)
    {
        return $container->make('location')->getCountry()->getName();
    }

    /**
     * @param Request $request
     * @param LocationLocator $locator
     * @return mixed
     */
    public function locate(Request $request, LocationLocator $locator)
    {
        return $locator->locate($request);
    }
}

License

This package is released under the MIT License. See the bundled LICENSE file for details.