divineomega/ laravel-geolocation-request
Laravel Geolocation Request
v1.1.0
2021-02-15 15:53 UTC
Requires
- php: >=7.1
- divineomega/do-file-cache-psr-6: ^2.0
- divineomega/php-geolocation: ^2.0
- laravel/framework: ^5.6||^6.0||^7.0||^8.0
This package is auto-updated.
Last update: 2024-09-15 23:59:23 UTC
README
Laravel Geolocation Request 包提供了一种简单的方法来通过调用 $request->country()
方法来定位请求的来源国家。
安装
要安装 Laravel Geolocation Request 包,只需运行以下 Composer 命令。
composer require divineomega/laravel-geolocation-request
使用方法
要在 Laravel 控制器方法中使用启用地理位置的请求,您可以在控制器顶部替换 use Illuminate\Http\Request
行,如下面的使用示例所示。
完成后,您可以通过调用 $request->country()
方法来执行地理位置定位并返回基于其 IP 地址的活动请求的来源国家。国家以包含许多属性的对象返回,例如国家名称和 ISO 代码。
<?php namespace App\Http\Controllers; //use Illuminate\Http\Request; use DivineOmega\LaravelGeolocationRequest\Http\Requests\GeolocationRequest as Request; class UserController extends Controller { /** * Store a new user. * * @param Request $request * @return Response */ public function store(Request $request) { // Perform geolocation, and return a country object. $country = $request->country(); $user = new \App\User(); $user->name = $request->name; $user->countryName = $country->name; $user->countryCode = $country->isoCodeAlpha3; $user->save(); // ... } }
如果您正在使用自定义请求对象,可以将它们更改为扩展提供的 GeolocationRequest
类。如果您无法扩展自定义请求对象,或者根本不想这样做,您可以通过使用提供的 GeolocatableRequest
trait 来添加地理位置功能。