> For the complete documentation index, see [llms.txt](https://shoaib-1.gitbook.io/sirjuni/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://shoaib-1.gitbook.io/sirjuni/request.md).

# Request

An abstraction over super-globals to make the interface clean.

The request class is provided as an abstraction over the usual Super Globals in PHP. To work with `GET, POST` we use the `$_GET` to get any query parameters and `$_POST` to get any data arriving using `POST` request. In the controllers, which handle the requests after the requests make it through the `Router` and `MiddleWare` if any is used, we can make use of the `Request` class's provided methods to get any data associated with the request.

For example: To get the value of an input field whose name attribute is `username` and is sent with a `POST` request, we can use the method `$request->formData['username']` which works the same as `$_POST['username']` but it also checks if the key `username` exists in the array, otherwise it returns `NULL`.

## Request instance in Controller

Every controller method should have a parameter `$request` which contains the current request instance. Using this instance, we can interact with the request and read its data easily.

```php
<?php
// request to path: /home/user?action=render

class homeController{
    public function index($request) {
        $url = $request->url();
        $context = ['url' => $url];
        VIEW::init('index.html', $context);    
        // init function extracts the $context array 
        // in the scope of index.html so that we can use these variables in the html file
    }
}
?>
```

## Methods supported by Request class

The Request class interface provides all the necessary methods to work with the Super global arrays. The complete list is given below with examples -

Lets create an instance of the Request class

```php
<?php   // index.php file
use sirJuni\Framework\Components\Request;

$request = new Request();

// below code goes here
?>
```

#### Methods List:

1. **fullUrl()**

> This method is used to return the full path that is being requested along with the query string. If we are running web server on localhost and we request localhost`/user/Admin?action=login` then, this method would return `/user/Admin?action=login`.
>
> ```php
> echo($request->fullUrl());    // echos /user/Admin/?action=login
> ```

2. **url()**

> This method is used to get just the requested path and not the query string. So, if we request the same path as above, we would get
>
> ```php
> echo($request->url());     // echos /user/Admin
> // remember the trailing slash after Admin, is removed automatically
> ```

3. **method()**

> It is used to get the method of the request like `GET, POST`.
>
> ```php
> echo($request->method());
> ```

4. **queryKeys()**

> This methods returns an array contains all the keys of $\_GET array.
>
> For example: if we make a request to `localhost/user/Admin/?name=shoaib&age=20` this method would return an array containing `['name', 'age']`.
>
> If no query string is in the path, it returns an empty array.
>
> ```php
> print_r($request->queryKeys());
> ```

5. **queryData($key)**

> This method is used to grab the corresponding value from $\_GET. Once we get the keys in the query string, we can iterate over them and grab the corresponding values for each key, in code it looks like:
>
> ```php
> foreach ($request->queryKeys() as $key) {
>     echo($request->queryData($key));
> }
> ```

6. **formKeys()**

> This method returns an array containing all the keys of $\_POST array, if its empty it returns an empty array `[]`.
>
> ```php
> print_r($request->formKeys());
> ```

7. **formData($key)**

> To get some value from the $\_POST array, if we know the key, then we can use this method to get the value.
>
> ```php
> echo($request->formData('username'));
> ```

For the `$_COOKIE` array, we have `cookieKeys()` and `cookieData($key)` methods.

For `$_SESSION` array, we have `sessionKeys()` and `sessionData($key)` methods.

This is all about the `Request` class and its methods.
