gerardojbaez/geodata
Composer 安装命令:
composer require gerardojbaez/geodata
包简介
Laravel 5.2 package that provides basic geographical data like Countries, Regions and Cities.
README 文档
README
GeoData is a Laravel package that provides basic geographical data like Countries, Regions and Cities.
Content
Installation
Composer
Pull this package through Composer (file composer.json)
{ "require": { "php": ">=5.5.9", "laravel/framework": "5.2.*", "gerardojbaez/geodata": "0.*" } }
Run this command inside your terminal.
composer update
Service Provider
Add the package to your application service providers in config/app.php file.
<?php 'providers' => [ [...] /** * Third Party Service Providers... */ Gerardojbaez\GeoData\GeoDataServiceProvider::class, ]
Migrations and Seeders
Publish package migrations and seeders with:
php artisan vendor:publish
Then run migrations.
php artisan migrate
If you want all countries, run:
php artisan db:seed --class AllCountriesSeeder
If you want specific countries, run:
php artisan db:seed --class Gerardojbaez\\GeoData\\Seeders\\UnitedStatesSeeder
php artisan db:seed --class Gerardojbaez\\GeoData\\Seeders\\PuertoRicoSeeder
[...]
Check available countries below.
Available Countries
| Countries |
|---|
| Afghanistan |
| Albania |
| Algeria |
| Andorra |
| Angola |
| Antigua and Barbuda |
| Argentina |
| Aruba |
| Australia |
| Austria |
| Bahamas |
| Barbados |
| Belgium |
| Belize |
| Bermuda |
| Bolivia |
| Brazil |
| Canada |
| Chile |
| China |
| Colombia |
| Costa Rica |
| Croatia |
| Cuba |
| Czech Republic |
| Denmark |
| Dominican Republic |
| Ecuador |
| El Salvador |
| Equatorial Guinea |
| Estonia |
| Finland |
| France |
| Germany |
| Greece |
| Greenland |
| Guatemala |
| Guyana |
| Haiti |
| Honduras |
| Hong Kong |
| India |
| Indonesia |
| Ireland |
| Israel |
| Italy |
| Ivory Coast |
| Jamaica |
| Japan |
| Latvia |
| Lebanon |
| Luxembourg |
| Malaysia |
| Mexico |
| Morocco |
| Netherlands |
| Nicaragua |
| Nigeria |
| Norway |
| Pakistan |
| Panama |
| Papua New Guinea |
| Paraguay |
| Peru |
| Philippines |
| Poland |
| Portugal |
| Puerto Rico |
| Romania |
| Russia |
| Saint Lucia |
| San Marino |
| Singapore |
| South Africa |
| Spain |
| Sri Lanka |
| Sweden |
| Switzerland |
| Thailand |
| Turkey |
| United Kingdom |
| United States |
| Uruguay |
| Venezuela |
Traits and Contracts
You can use GeoData traits when you need to define a relation to countries, regions and/or cities.
See the following example:
<?php namespace App\Models; // [...] use Gerardojbaez\GeoData\Contracts\HasCountryContract; use Gerardojbaez\GeoData\Contracts\HasRegionContract; use Gerardojbaez\GeoData\Contracts\HasCityContract; use Gerardojbaez\GeoData\Traits\HasCountry; use Gerardojbaez\GeoData\Traits\HasRegion; use Gerardojbaez\GeoData\Traits\HasCity; class User extends Model implements HasCountryContract, HasRegionContract, HasCityContract { use HasCountry, HasRegion, HasCity; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'country_code', 'region_id', 'city_id', ... ];
Usage
Installing Countries
In addition to install countries via command line using seeders, you can also
install countries through the CountryInstaller class. This will help you create a "web interface" that will give your customers or users the ability to install countries as needed. The installer will check if the country has been already installed and in that case will throw Gerardojbaez\Geodata\Exceptions\CountryAlreadyInstalledException.
The installation include:
- The country
- Country Regions
- Country Cities
It's as simple as:
<?php use Gerardojbaez\Geodata\CountryInstaller; // Install United States. $installer = new CountryInstaller('United States'); $installer->install(); // Install Puerto Rico. $installer = new CountryInstaller('Puerto Rico'); $installer->install(); // Install Spain $installer = new CountryInstaller('Spain'); $installer->install();
Models
This package comes with Gerardojbaez\GeoData\Models\Country,
Gerardojbaez\GeoData\Models\Region and
Gerardojbaez\GeoData\Models\City models.
Take a look at each model for more details.
Controllers
If you want to provide countries, regions and/or cities data to your frontend you may want to use
Gerardojbaez\GeoData\Controllers\CountriesController,
Gerardojbaez\GeoData\Controllers\RegionsController and
Gerardojbaez\GeoData\Controllers\CitiesController controllers.
Controllers returns a json reponse containing (if any) the requested data.
Routes
This is an example. You can structure these routes as you want.
<?php // Show countries list. Route::get('api/geo/countries', [ 'uses' => '\Gerardojbaez\GeoData\Controllers\CountriesController@countries', 'as' => 'geodata.countries' ]); // Show regions list. Route::get('api/geo/{country}/regions', [ 'uses' => '\Gerardojbaez\GeoData\Controllers\RegionsController@regions', 'as' => 'geodata.regions' ]); // Show cities list. Route::get('api/geo/{country}/{region}/cities', [ 'uses' => '\Gerardojbaez\GeoData\Controllers\CitiesController@cities', 'as' => 'geodata.cities' ]);
License
This package is free software distributed under the terms of the MIT license.
gerardojbaez/geodata 适用场景与选型建议
gerardojbaez/geodata 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.99k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2016 年 07 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「countries」 「geographical data」 「cities」 「regions」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gerardojbaez/geodata 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gerardojbaez/geodata 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gerardojbaez/geodata 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Geographical functionality for ORM and ODM entities and JavaScript maps rendering
List of all countries with names and ISO 3166-1 codes in all languages and data formats for Laravel
A package to delivery a wide seed array of Countries & their respective cities.
GeoNames.org geographical data and associated functionality
Geo module with countries and cities
统计信息
- 总下载量: 11.99k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 32
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-22