[slimPHP](Ver3)の基本的ルートの設定

#基本設定

```〇〇.php
<?php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
require "vendor/autoload.php";
use \Slim\App;

$configuration = [
    'displayErrorDetails' => true //エラーの情報をえるか？　開発は欲しいのでtrue 本番はエラーをユーザーに表示するのは危険なのでfalse
];

$app = new \Slim\App($configuration);

/*
以下記述はここにかく
*/

$app->run();
```

#指定パスのみ

```〇〇.php
//methoがGETでPassがhelloの場合
$app->get('/hello', function (ServerRequestInterface $request, ResponseInterface  $response, $args = []) {
    $response->getBody()->write("Hello");
    return $response;
});

//methoがPOSTでPassがhelloの場合
$app->post('/hello', function (ServerRequestInterface $request, ResponseInterface  $response, $args = []) {
    $response->getBody()->write("Hello");
    return $response;
});
```
出力:
<img width="316" alt="スクリーンショット 2019-06-16 13.41.32.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/f89bf833-17e2-954c-d890-c1c0ee56ea4f.png">

#指定パス+任意パス
```〇〇.php
$app->get('/hello/{name}', function (ServerRequestInterface $request, ResponseInterface  $response, $args = []) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});
```
出力:
<img width="353" alt="スクリーンショット 2019-06-16 13.47.08.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/a27292aa-9019-a0ee-be09-084d7688b838.png">

#指定パス+任意パス(正規表現)

```〇〇.php
$app->get('/user/{id:[0-9]+}', function (ServerRequestInterface $request, ResponseInterface  $response, $args = []) {
    $id = $args['id'];
    $response->getBody()->write("$id, のUserさん");
    return $response;
});
```

出力:
<img width="334" alt="スクリーンショット 2019-06-16 13.53.43.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/6ba0cb25-cea3-3169-c223-02f02bb9156f.png">

<img width="431" alt="スクリーンショット 2019-06-16 13.53.50.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/0937f41e-b60c-4b4e-43f3-c4e39d103020.png">

#指定パス+任意パス達(あってもなくても通る)

```〇〇.php
$app->get('/news[/{year}[/{month}]]', function ($request, $response, $args) {
    $year = $args['year'] ?: 'year';
    $month = $args['month'] ?: 'month';
    $response->getBody()->write("本日のNews, $year, $month");
    return $response;
});
```
出力:
<img width="368" alt="スクリーンショット 2019-06-16 14.07.16.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/ffdd1231-9f99-b139-a6fc-3379ad0aa2b5.png">
<img width="354" alt="スクリーンショット 2019-06-16 14.07.23.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/2e53af49-c0dd-17f2-2664-9ecc72f9221f.png">
<img width="368" alt="スクリーンショット 2019-06-16 14.07.38.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/6cb481bc-7f32-a162-9b52-aff8e53f5206.png">

#クエリ取得
```〇〇.php
$app->get('/samples', function (ServerRequestInterface $request, ResponseInterface $response,  $res, $args = []) {
    return '!' . implode( $request->getQueryParams());
});
```
出力:
<img width="563" alt="スクリーンショット 2019-06-16 14.35.54.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/198f816c-5b6c-716c-acab-4dee786a5c80.png">

※単一で取得したい場合
`$myvar = $request->getQueryParams()["myvar"]; //Get 取得`

※postで取得したい場合
` $request->getParsedBody()`

#ヘッダー取得

```〇〇.php
$app->get('/samples', function (ServerRequestInterface $request, ResponseInterface $response,  $res, $args = []) {
    $headers = $request->getHeaders();
    foreach ($headers as $name => $values) {
        echo $name . ": " . implode(", ", $values);
    }
    return '!';
});
```
出力:
<img width="733" alt="スクリーンショット 2019-06-16 14.44.06.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/059128f5-d53d-7a38-a555-731bdffc930e.png">


#複数method対応

```〇〇.php
app->map(['GET', 'POST'], '/book', function ($request, $response, $args) {
  $response->getBody()->write("book!!");
  return $response;
});
```
※methos取得したい場合(文字列)
` $method = $request->getMethod();`

#出力方法(JSONで)

```〇〇.php
$app->get('/samplesample', function ($request, $response, $args) {
    $data = array('name' => 'Bob', 'age' => 40);
    $newResponse = $response->withJson($data);
    return $newResponse;
});
```
出力:
<img width="362" alt="スクリーンショット 2019-06-16 14.19.32.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/996ab583-83d1-c0a0-5530-ee10dc2e3ddb.png">

#出力方法(HTMLタグ使用)

```〇〇.php
$app->get('/samplesample', function ($request, $response, $args) {
    $url = 'https://www.php.net';
    $response->write("<a href='$url'>PHPページに飛びます</a>");
    return $response;
});
```
出力:
<img width="379" alt="スクリーンショット 2019-06-16 14.23.15.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/fb3d959d-8b1c-d309-33a7-6b5e361383ee.png">

#group作成

```〇〇.php
$app->group('/users/{id:[0-9]+}', function () {
    $this->group('/auth', function () {
        $this->map(['GET', 'POST'], '/books', function ($request, $response, $args) {
            return $response->write("ppppp-");
        });
    });

    $this->group('/a', function () {
        $this->map(['GET', 'POST'], '/books', function ($request, $response, $args) {
            return $response->write("ppppp1");
        });
    });
});
```

出力:
<img width="432" alt="スクリーンショット 2019-06-16 14.46.19.png" src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/133940/4ddcc416-b4f9-ae98-8181-59d18e30bcbd.png">

参考：
http://www.slimframework.com/docs/
https://discourse.slimframework.com/t/slim-3-routing-best-practices-and-organization/93/3