imo-tikuwa/cakephp-zipcode-jp

CakePHP 的 ZipcodeJp 插件

安装: 123

依赖者: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

公开问题: 0

类型:cakephp-plugin

v2.2.0 2022-12-17 07:25 UTC

README

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方式是

# for CakePHP4
composer require imo-tikuwa/cakephp-zipcode-jp "2.*"

# for CakePHP3
composer require imo-tikuwa/cakephp-zipcode-jp "1.*"

在 Application.php 中加载插件。

class Application extends BaseApplication
{
    public function bootstrap()
    {
        $this->addPlugin('ZipcodeJp', ['routes' => true]);
    }
}

初始化

执行迁移以创建 zipcode_jps 表。
当您执行 initialize_zipcode_jp 命令时,将获取并存储日本邮政的最新邮政编码数据到数据库中。

bin\cake.bat migrations migrate --plugin ZipcodeJp
bin\cake.bat initialize_zipcode_jp

(服务器端)使用方法

$zipcode = '1010032';

$this->ZipcodeJps = $this->fetchTable('ZipcodeJp.ZipcodeJps');
$result = $this->ZipcodeJps->findByZipcode($zipcode);

(客户端)使用方法

<input type="text" id="zipcode" maxlength="7" placeholder="ここに郵便番号を入力" />
$("#zipcode").on("keyup", function(){
    $("#pref").text('');
    $("#city").text('');
    $("#address").text('');
    if ($(this).val().length == 7) {
        let requesturl = '/zipcode-jp/' + $(this).val() + '.json';
        $.ajax({
            type: 'get',
            url: requesturl,
            dataType: 'json'
        }).done(function(result) {
            if (result != null) {
                $("#pref").text(result['pref']);
                $("#city").text(result['city']);
                $("#address").text(result['address']);
            }
        });
    }
});