Edited at

[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;
});


出力:


指定パス+任意パス


〇〇.php

$app->get('/hello/{name}', function (ServerRequestInterface $request, ResponseInterface  $response, $args = []) {

$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});

出力:


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


〇〇.php

$app->get('/user/{id:[0-9]+}', function (ServerRequestInterface $request, ResponseInterface  $response, $args = []) {

$id = $args['id'];
$response->getBody()->write("$id, のUserさん");
return $response;
});

出力:


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


〇〇.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;
});

出力:






クエリ取得


〇〇.php

$app->get('/samples', function (ServerRequestInterface $request, ResponseInterface $response,  $res, $args = []) {

return '!' . implode( $request->getQueryParams());
});

出力:

※単一で取得したい場合

$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 '!';
});

出力:


複数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;
});

出力:


出力方法(HTMLタグ使用)


〇〇.php

$app->get('/samplesample', function ($request, $response, $args) {

$url = 'https://www.php.net';
$response->write("<a href='$url'>PHPページに飛びます</a>");
return $response;
});

出力:


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");
});
});
});


出力:

参考:

http://www.slimframework.com/docs/

https://discourse.slimframework.com/t/slim-3-routing-best-practices-and-organization/93/3