imo-tikuwa / cakephp-zipcode-jp
CakePHP 的 ZipcodeJp 插件
v2.2.0
2022-12-17 07:25 UTC
Requires
- php: >=8.0
- cakephp/cakephp: >=4.3
This package is auto-updated.
Last update: 2024-09-17 11:30:03 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']); } }); } });