# HTTP 소개

## 📌 Request <a href="#request" id="request"></a>

`Form`을 작성하거나, `File`을 업로드 하는 등의 사용자 요청과 관련된 제어를 수행합니다. 일반적으로 `$_GET`, `$_POST`, `$_FILES`, `$_SERVER`, `$_REQUEST`의 내용에 접근할 수 있으며, 아래 명시된 편의 메서드를 제공합니다.

```php
Request::get()->get('someKey'); // $_GET['someKey']
Request::post()->get('someKey'); // $_POST['someKey']
Request::server()->get('REQUEST_URI'); // $_SERVER['REQUEST_URI']
Request::files()->get('someKey'); // $_FILES['someKey']
Request::request()->get('someKey'); // $_REQUEST['someKey']
```

> 단, `$_ENV` 는 지원하지 않습니다. 다차원 키의 접근 방법으로 `dot notation` 을 지원합니다.

### **값 설정하기**

```php
Request::get()->set('name', 'value');
Request::post()->set('name', 'value');
Request::files()->set('file.name', 'value');
Request::request()->set('sampel.name', 'value');
Request::server()->set('REQUEST_URI', 'value');
```

### **값 가져오기**

```php
$name = Request::get()->get('name');
$name = Request::post()->get('name.first');
$name = Request::files()->get('name.first');
$name = Request::request()->get('name.first');
$name = Request::server()->get('REQUEST_URI');
```

요청값이 없는 경우 다음과 같이 2번째 인자로, 기본값을 설정할 수 있습니다.

```php
$name = Request::get()->get('name', '기본값');
$name = Request::post()->get('name.first', '기본값');
```

### **모든 값 가져오기**

해당 `get()`이 가지고 있는 모든 변수를 배열로 반환합니다.

```php
$allGet = Request::get()->all();
$allGet = Request::get()->toArray();
```

### **값 지우기**

```php
Request::get()->del('name');
Request::get()->del('name.first');
```

### **모든 값 지우기**

```php
Request::get()->clear();
```

### **값의 존재유무 체크하기**

```php
if (Request::get()->has('name')) {
    // some code...
}

if (Request::get()->has('name.first')) {
    // some code...
}
```

### **주요 Method**

#### **getServerAddress()**

서버의 `IP주소`를 반환하며, `cli`에서도 정상적으로 가져올 수 있도록 처리되었습니다.

#### **getRemoteAddress()**

클라이언트의 `ip주소`를 반환하며, `Proxy` 혹은 `nginx`와 같은 구조에서 정상적으로 반환할 수 있도록 처리되었습니다.

#### **isCli()**

`cli`접근 여부를 체크하여 `boolean`을 반환합니다.

```php
if (Request::isCli()) {
    // background process
}
```

#### **isAjax()**

`Request` 가 `ajax`인지 체크 후 `boolean` 값을 반환합니다.

```php
if (Request::isAjax()) {
    // ajax code
}
```

#### **isMethod($method)**

주어진 `$method` 인자와 현재 `REQUEST_METHOD` 비교 후 `boolean` 값을 반환하며, 파라미터가 `get`, `post`, `head`, `options`, `put`, `delete`, `trace`, `connect`이 아닌 경우 예외 처리됩니다.

```php
if (Request::isMethod('get')) {
    // get인 경우만 실행
}

if (Request::isMethod('post')) {
    // get인 경우만 실행
}
```

#### **isMobile()**

`도메인`의 `m`이 있는지 여부를 통해 모바일 여부를 `boolean` 형태로 반환합니다.

```php
if (Request::isMobile()) {
    // 도메인이 m.domain.com 인 경우
}
```

#### **isMobileDevice()**

접속한 기기가 `모바일` or `태블릿`인지 `UserAgent`를 통해 체크 후 `boolean`을 반환합니다.

```php
if (Request::isMobileDevice()) {
    // 접속기기가 모바일/태블릿인 경우
}
```

#### **isRefresh()**

브라우저에서 `새로고침`을 했는지에 대해 체크 후 `boolean`을 반환합니다.

```php
if (Request::isRefresh()) {
    // 페이지 새로고침을 한 경우
}
```

## 📌 Response <a href="#response" id="response"></a>

`Json`, `Redirect`, `Streamed`, `BinaryFile` 의 4가지 형태로 응답 객체를 생성할 수 있습니다. Chaining method를 제공합니다.

### **Create Instance**

```php
$response = Response::create($content, $statusCode, $headers);
```

## 📌 Session <a href="#session" id="session"></a>

파일 세션만 지원합니다.

> 다차원 키의 접근 방법으로 `dot notation` 을 지원합니다.

### **Setting a value**

```php
Session::set('name', 'value');
Session::set('name.first', 'value');
```

### **Getting a value**

```php
$name = Session::get('name');
$name = Session::get('name.first');
```

> 2번째 인자로, 기본값을 설정할 수 없습니다. 미설정시 `null` 로 설정됩니다.

### **Getting all values**

```php
$session = Session::all();
```

### **Removing a value**

```php
Session::del('name');
Session::del('name.first');
```

### **Removing all values**

```php
Session::clear();
```

### **Determining if an given key exists**

```php
if (Session::has('name')) {
    // some code...
}

if (Session::has('name.first')) {
    // some code...
}
```

## 📌 Cookie <a href="#cookie" id="cookie"></a>

> 쿠키는 다차원 키를 사용할 수 없습니다.

### **Setting a value**

```php
Cookie::set('name', 'value'); // 세션 쿠키 생성
Cookie::set('name', 'value', 3600, '/', true, true); // 1시간 만료 쿠키 생성
```

### **Getting a value**

```php
$name = Cookie::get('name');
```

### **Getting all values**

```php
$session = Cookie::all();
```

### **Removing a value**

```php
Cookie::del('name');
```

### **Determining if an given key exists**

```php
if (Cookie::has('name')) {
    // some code...
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devcenter-help.nhn-commerce.com/guide/preparation/detailed-structure/http.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
