PHP code example of megakit / laravel-location
1. Go to this page and download the library: Download megakit/laravel-location library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
megakit / laravel-location example snippets
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' => [
//
],
],
],
];
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);
}
}