https://github.com/technically-php/rack-utils-http-query
Tiny PHP package to build HTTP URL query string compatible with Rails apps
https://github.com/technically-php/rack-utils-http-query
Last synced: 11 months ago
JSON representation
Tiny PHP package to build HTTP URL query string compatible with Rails apps
- Host: GitHub
- URL: https://github.com/technically-php/rack-utils-http-query
- Owner: technically-php
- License: mit
- Created: 2016-11-01T20:27:09.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-01T20:51:45.000Z (over 9 years ago)
- Last Synced: 2024-12-07T00:40:47.357Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 11.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RackUtils HTTP query
Tiny package to build HTTP URL query string compatible with Rails app.
# Usage
This library produces output without explicit indices. As simple as that.
``` php
$vars = ['fruits' => ['apple', 'banana', 'orange']];
$query_string = \TechnicallyPhp\RackUtilsHttpQuery::build($vars);
var_dump($query_string);
// will output: "fruits[]=apple&fruits[]=banana&fruits[]=orange"
```
# Motivation
PHP stock [`http_build_query()`](http://php.net/manual/en/function.http-build-query.php)
converts array into items with indices explicitly defined.
This is not compatible with the way Rails applications parse requests.
``` php
$vars = ['fruits' => ['apple', 'banana', 'orange']];
$query_string = http_build_query($vars);
var_dump($query_string);
// will output: "fruits[0]=apple&fruits[1]=banana&fruits[2]=orange"
```
Relevant StackOverflow discussions:
1. [php url query nested array with no index](http://stackoverflow.com/questions/11996573/php-url-query-nested-array-with-no-index)
# Details
This package follows
`Rack::Utils.parse_nested_query` [specification](https://github.com/rack/rack/blob/master/test/spec_utils.rb).
Please check [RackUtilsHttpQueryTest.php](./tests/RackUtilsHttpQueryTest.php#L59).