定制 thecoder/world 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thecoder/world

Composer 安装命令:

composer require thecoder/world

包简介

World location data

README 文档

README

World is a powerful PHP and Laravel package for retrieving structured geographical data, including continents, countries, provinces, and cities. This package is lightweight, optimized for performance, and seamlessly integrates with Laravel applications.

🚀 Features

✔ Get all continents, countries, provinces, and cities
✔ Filter countries by continent
✔ Retrieve provinces and cities for any country
✔ Optimized for high performance with caching
✔ Compatible with Laravel and PHP
✔ Includes migrations and seeding for easy setup
✔ includes a Laravel Facade for simpler usage!

📦 Installation

Install via Composer:

composer require thecoder/world

If auto-discovery is not working, manually add the service provider in config/app.php:

TheCoder\World\WorldServiceProvider::class,

🔧 Publish Configuration & Migrations for Greater Flexibility

php artisan vendor:publish --provider="TheCoder\World\WorldServiceProvider"

This will publish:
config/world.php (configuration file)
✅ Database migration files

🛠 Migrate the Database

php artisan migrate

🌍 Seed Geographical Data

php artisan world:seed

This will populate the database with continents, countries, provinces, and cities.

The data is downloaded once from dr5hn/countries-states-cities-database and cached locally in database/seeders/countries+states+cities.json. Re-running world:seed upserts records by a stable source_id, so existing rows keep their id — safe to run again after the upstream data changes.

To pull the latest upstream data instead of reusing the local cache:

php artisan world:seed --fresh-data

🔍 Usage

1️⃣ Using the Built-in World Facade (Recommended)

The package now includes a World Facade, making it easier to access data:

use World;

$continents = World::continents()->get();  // Get all continents
$countries = World::countries()->get();  // Get all countries

2️⃣ Manually Instantiate the Class

use TheCoder\World;

$world = new World();

$continents = $world->continents()->get();  
$countries = $world->countries()->get();  

Get Locations

$location = World::byId(1)->first();

$location = World::byEnglishName('Asia')->first();

$locations = World::byIds([1,2])->get();

$locations = World::byEnglishNames(['Iraq', 'Iran'])->get();

$location = World::count();  

Get Countries by Continent

$asia = World::continents('Asia')->first();

$asia = World::continents()->byEnglishName('Asia')->first();  

$asiaCountries = World::continents()->byEnglishName('Asia')->countries()->get();  

Get Provinces & Cities

$iranProvinces = World::countries('Iran')->provinces()->get();

$iranProvinces = World::countries()->byEnglishName('Iran')->provinces()->get();

$gilanCities = World::provinces('Gilan')->cities()->get();  
  
$gilanCities = World::provinces()->byEnglishName('Gilan')->cities()->get();  

⚡Caching for Maximum Performance

Since the World database is static and never changes, this package supports permanent caching to eliminate redundant database queries and significantly improve performance.

Enable Caching

You can enable caching in the config/world.php file:

  'cache' => [
      'enabled' => true,
      'prefix' => 'thecoder-world-',
      'tag' => 'thecoder-world',
      'ttl' => null, // Store cache forever
  ],

Setting ttl to null ensures that data is cached forever.

Use Cached Data in Queries

If caching is enabled, queries will automatically store results in the cache forever.

Manually Clear Cache (If Needed)

To clear the cache manually, run:

  php artisan cache:clear

Or in code:

World::clearCache();

🏗 Package-Integrated Laravel Facade

The package now provides a World Facade out-of-the-box, meaning Laravel users don't need to set it up manually.

No need to register aliases
Works automatically in Laravel

Just install the package and start using it:

use World;

$continents = World::continents()->get();

🧪Testing

Ensure you have the necessary dependencies installed:

composer install --dev

⚠️ Database Configuration for Testing

Due to special columns in the database, SQLite is not supported for testing.

Instead, configure MySQL in phpunit.xml:

    <php>
        <env name="DB_CONNECTION" value="mysql"/>
        <env name="DB_HOST" value="127.0.0.1"/>
        <env name="DB_PORT" value="3306"/>
        <env name="DB_DATABASE" value="world_test_database"/>
        <env name="DB_USERNAME" value="root"/>
        <env name="DB_PASSWORD" value=""/>
    </php>

▶️ Running tests

vendor/bin/phpunit

🗄 Database Schema

This package provides a locations table designed to store structured geographic data with hierarchical relationships.

Column Type Description
id Integer (PK) Unique identifier
continent_id Integer (FK) Parent continent (nullable)
country_id Integer (FK) Parent country (nullable)
province_id Integer (FK) Parent province (nullable)
iso_code String (3) ISO country code (e.g., "US")
type Enum continent, country, province, city
english_name String Location name in English
native_name String Local name (optional)
timezone String Time zone (e.g., "Asia/Tehran")
is_capital Boolean Marks capital cities (default: false)
priority Integer Sort priority (default: 0)
center POINT Geographic coordinates
area MULTIPOLYGON Spatial data for regions

Indexed for fast queries
Supports geographic coordinates & boundaries

❓ FAQ

🔹 What is this package used for?

It helps developers retrieve geographical data (continents, countries, provinces, and cities) for Laravel & PHP applications.

🔹 Is this package compatible with Laravel?

Yes! It fully supports Laravel and can also be used in vanilla PHP projects.

🔹 How can I filter countries by continent?

World::continents('Europe')->countries()->get();

🔹 Can I get cities of a specific province?

World::provinces('California')->cities()->get();

⭐ Contribute & Support

🔹 GitHub Repository: thecoder/world
🔹 Issues & Features: Submit Here
🔹 Contribute: Fork, star ⭐, and submit PRs

💡 Need more features? Open an issue or contribute to the project!

thecoder/world 适用场景与选型建议

thecoder/world 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 65 次下载、GitHub Stars 达 22, 最近一次更新时间为 2025 年 02 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 thecoder/world 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 thecoder/world 我们能提供哪些服务?
定制开发 / 二次开发

基于 thecoder/world 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 65
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 22
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 22
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-14