PHP code example of youvision / think-regions

1. Go to this page and download the library: Download youvision/think-regions 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/ */

    

youvision / think-regions example snippets


// config/regions.php
'route' => false,

use think\facade\Route;
use Youvision\ThinkRegions\RegionController;

Route::group('regions', function () {
    Route::rule('getRegion', RegionController::class . '@getRegion', 'GET|POST');
    Route::rule('getProvince', RegionController::class . '@getProvince', 'GET|POST');
    Route::rule('getCity', RegionController::class . '@getCity', 'GET|POST');
    Route::rule('getDistrict', RegionController::class . '@getDistrict', 'GET|POST');
    Route::rule('getStreet', RegionController::class . '@getStreet', 'GET|POST');
    Route::rule('searchRegion', RegionController::class . '@searchRegion', 'GET|POST');
});

use Youvision\ThinkRegions\Facade\Regions;

Regions::getProvince();
Regions::getCity(1);
Regions::getDistrict(2);
Regions::getStreet(3);
Regions::searchProvince('广东');
Regions::searchCity('sz', 1);
Regions::getById(4);
Regions::getByAdcode('440515');
Regions::getPath(4);
Regions::getAddress(4, '/');
Regions::getLevelName(4);

use Youvision\ThinkRegions\Regions;

class Address
{
    public function __construct(protected Regions $regions)
    {
    }

    public function cities(int $provinceId): array
    {
        return $this->regions->getCity($provinceId);
    }
}

use Youvision\ThinkRegions\Regions;

$regions = new Regions([
    'table'  => 'regions',
    'cache'  => false,
    'order'  => 'name asc',
]);

$provinces = $regions->getProvince();
bash
php think service:discover
bash
php think regions:install
bash
# 删除并重建数据表后重新导入
php think regions:install --force

# 使用本地 CSV 文件
php think regions:install --file=/data/regions.csv

# 临时指定远程 CSV 地址
php think regions:install --url=http://example.com/regions.csv

# 调整每批写入条数
php think regions:install --chunk=1000
bash
php think regions:publish
bash
composer install
php -d extension=pdo_sqlite -d extension=sqlite3 vendor/bin/phpunit