christianvermeulen/postalcode-bundle

此扩展包通过 https://api.postcode.nl 提供荷兰地址的检索和验证。

dev-master 2013-08-10 00:03 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:16:01 UTC


README

此扩展包提供了在 Symfony2 中使用 https://api.postcode.nl 的包装器。

安装

当使用 composer 时,在您的 composer.json 中添加以下内容

// composer.json
{
    //...

    "require": {
        //...
        "christianvermeulen/postalcode-bundle" : "dev-master"
    }

    //...
}

并运行 php composer.phar update christianvermeulen/postalcode-bundle

接下来,在您的 appkernel 中添加以下内容

    // in AppKernel::registerBundles()
    $bundles = array(
        // Dependencies
        new ChristianVermeulen/PostalcodeBundle/ChristianVermeulenPostalcodeBundle();
    );

配置

将以下内容添加到您的 app/config/config.yml

    christian_vermeulen_postalcode:
        key: [your_key]
        secret: [your_secret]

您可以通过在 https://api.postcode.nl/ 注册您的网店来获得这些信息。

如果您想支持 Ajax 请求,请添加路由:app/config/routing.yml

    christian_vermeulen_postalcode:
        resource: "@ChristianVermeulenPostalcodeBundle/Resources/config/routing.yml"
        prefix: /postal

使用方法

每次需要查找地址时,您有两个选择。

  • 通过服务
  • 通过 POST 请求

通过服务

在任何控制器中,您可以使用以下代码

    // get the api service
    $api = $this->get('christianvermeulen_postalcode');

    // Look up the address for Dutch postcode 2012ES, housenumber 30,
    // with no housenumber addition.
    try
    {
        $address = $client->lookupAddress('2012ES', '30', '');
    }
    catch (PostcodeNl_Api_RestClient_AddressNotFoundException $e)
    {
        die('There is no address on this postcode/housenumber combination: '. $e);
    }
    catch (PostcodeNl_Api_RestClient_InputInvalidException $e)
    {
        die('We have input which can never return a valid address: '. $e);
    }
    catch (PostcodeNl_Api_RestClient_ClientException $e)
    {
        die('We have a problem setting up our client connection: '. $e);
    }
    catch (PostcodeNl_Api_RestClient_AuthenticationException $e)
    {
        die('The Postcode.nl API service does not know who we are: '. $e);
    }
    catch (PostcodeNl_Api_RestClient_ServiceException $e)
    {
        die('The Postcode.nl API service reported an error: '. $e);
    }

    // Print the address data ($address is an array)
    echo var_export($address, true);

通过 POST 请求

如果您已包含此扩展包的路由,则可以对名为 christianvermeulen_postalcode_bundle 的路由执行 POST 请求。您需要传递以下参数

  • postalcode
  • number
  • addition

然后您将获得一个包含正确详情的 JsonResponse。在您的表单中的示例 JavaScript 可能如下所示

    // Get the data to post through ajax
    var postalcode = $("#postalcode").val();
    var housenumber = $("#housenumber").val();
    var addition = $("#addition").val();

    // Perform ajax to get address
    $.ajax({
      url: "{{ path('christian_vermeulen_postalcode') }}",
      data: { postalcode: postalcode, number: housenumber, addition: addition },
      type: "post",
      dataType: "json"
    })

    // What should happen when we get the adress
    .done(function(response){
        // set the data in the fields
        $("#smaakwereld_sitebundle_addresstype_street").val(response.street);
        $("#smaakwereld_sitebundle_addresstype_city").val(response.city);
    })

    // What should happen if we fail?
    .fail(function(){
      console.log("We've encountered a problem!");
    });

合作

请随意 fork 此存储库并添加/改进功能。任何 pull 请求和/或问题都非常受欢迎!

MIT 许可证 (MIT)

版权所有 (c) 2013 Christian Vermeulen

特此授予任何人免费获得此软件及其相关文档文件(以下简称“软件”)副本的权限,以不受限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向提供软件的个人提供这样做,前提是以下条件

上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论这些责任是基于合同、侵权或其他方式,无论这些责任是否源于、因之或与此软件或软件的使用或其他方式有关。