# Query Params Slim x Angular 4

**URL:** <https://discourse.slimframework.com/t/query-params-slim-x-angular-4/2396>\
**Category:** Questions\
**Created:** [May 8, 2018, 10:02am UTC](https://discourse.slimframework.com/t/query-params-slim-x-angular-4/2396 "2018-05-08T10:02:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elvisreis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/elvisreis/32/1298_2.png) [@elvisreis](https://discourse.slimframework.com/u/elvisreis)\
**Post date:** [May 8, 2018, 10:02am UTC](https://discourse.slimframework.com/t/query-params-slim-x-angular-4/2396/1 "2018-05-08T10:02:31Z")

</div>

Angular 4 per pattern transforms an array object with multiple values as follows **status** =approved& **status** =handling has some way to retrieve this array in the slim

/api/order?start=2018-04-08&end=2018-05-08&status=approved&status=handling&limit=10&offset=0

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 8, 2018, 8:26pm UTC](https://discourse.slimframework.com/t/query-params-slim-x-angular-4/2396/2 "2018-05-08T20:26:14Z")

</div>

Hi @elvisreis

By default this is not possible with PHP or Slim. BUT you could use this function to parse the Query string like you expected:

### Code

```php
function proper_parse_str($str)
{
    // result array
    $arr = [];

    // split on outer delimiter
    $pairs = explode('&', $str);

    // loop through each pair
    foreach ($pairs as $i) {
        // split into name and value
        list($name, $value) = explode('=', $i, 2);

        // if name already exists
        if (isset($arr[$name])) {
            # stick multiple values into an array
            if (is_array($arr[$name])) {
                $arr[$name][] = $value;
            } else {
                $arr[$name] = [$arr[$name], $value];
            }
        } else {
            // otherwise, simply stick it in a scalar
            $arr[$name] = $value;
        }
    }

    // return result array
    return $arr;
}

```

[Source](http://php.net/manual/en/function.parse-str.php#76792)

### Usage

```auto
$queryString = $request->getServerParam('QUERY_STRING');
$params = proper_parse_str($queryString);

```

Putting this into a middleware would also be possible.

### Result

![image](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/1X/bc143b8041ab58847c164fdd727f4f15ddd13aa2.png)

---

<div class="post-metadata">

**Author:** ![elvisreis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/elvisreis/32/1298_2.png) [@elvisreis](https://discourse.slimframework.com/u/elvisreis)\
**Post date:** [May 9, 2018, 10:30am UTC](https://discourse.slimframework.com/t/query-params-slim-x-angular-4/2396/3 "2018-05-09T10:30:36Z")

</div>

This and exactly what I need I just did not understand how to return to the slim the modified query string

```auto
<?php
/**
 * Created by PhpStorm.
 * User: elvis
 * Date: 09/05/2018
 * Time: 07:06
 */

namespace App\Middleware;

use Slim\Http\Request;
use Slim\Http\Response;

class QueryStringMiddleware
{
    /**
     * Example middleware invokable class
     *
     * @param \Slim\Http\Request $request PSR7 request
     * @param \Slim\Http\Response $response PSR7 response
     * @param callable $next Next middleware
     *
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function __invoke(Request $request, Response $response, $next)
    {
        $queryString = $request->getServerParam('QUERY_STRING');
        $queryString = $this->proper_parse_str($queryString);
        $_SERVER['QUERY_STRING'] = http_build_query($queryString);
        $response = $next($request, $response);
        return $response;
    }

    private function proper_parse_str($str): array
    {
        // result array
        $arr = [];

        // split on outer delimiter
        $pairs = explode('&', $str);

        // loop through each pair
        foreach ($pairs as $i) {
            // split into name and value
            list($name, $value) = explode('=', $i, 2);

            // if name already exists
            if (isset($arr[$name])) {
                # stick multiple values into an array
                if (is_array($arr[$name])) {
                    $arr[$name][] = $value;
                } else {
                    $arr[$name] = [$arr[$name], $value];
                }
            } else {
                // otherwise, simply stick it in a scalar
                $arr[$name] = $value;
            }
        }

        return $arr;
    }
}

```

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [May 9, 2018, 11:59am UTC](https://discourse.slimframework.com/t/query-params-slim-x-angular-4/2396/4 "2018-05-09T11:59:38Z")

</div>

Ok then try this:

1. Rename the class method `proper_parse_str` to `parseQueryString`
2. Change the middleware `__invoke` method as follows:

```php
public function __invoke(Request $request, Response $response, $next)
{
    $queryString = $request->getServerParam('QUERY_STRING');
    $queryParams = $this->parseQueryString($queryString);
    $request = $request->withQueryParams($queryParams);
    
    return $next($request, $response);
}

```

Then read the query parameters in your controller / action as follows:

```php
$status = $request->getQueryParam('status');

```
