LoginSignup
0
0

More than 3 years have passed since last update.

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

Last updated at Posted at 2019-06-16

基本設定

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

出力:
スクリーンショット 2019-06-16 13.41.32.png

指定パス+任意パス

〇〇.php
$app->get('/hello/{name}', function (ServerRequestInterface $request, ResponseInterface  $response, $args = []) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});

出力:
スクリーンショット 2019-06-16 13.47.08.png

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

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

出力:
スクリーンショット 2019-06-16 13.53.43.png

スクリーンショット 2019-06-16 13.53.50.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;
});

出力:
スクリーンショット 2019-06-16 14.07.16.png
スクリーンショット 2019-06-16 14.07.23.png
スクリーンショット 2019-06-16 14.07.38.png

クエリ取得

〇〇.php
$app->get('/samples', function (ServerRequestInterface $request, ResponseInterface $response,  $res, $args = []) {
    return '!' . implode( $request->getQueryParams());
});

出力:
スクリーンショット 2019-06-16 14.35.54.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 '!';
});

出力:
スクリーンショット 2019-06-16 14.44.06.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;
});

出力:
スクリーンショット 2019-06-16 14.19.32.png

出力方法(HTMLタグ使用)

〇〇.php
$app->get('/samplesample', function ($request, $response, $args) {
    $url = 'https://www.php.net';
    $response->write("<a href='$url'>PHPページに飛びます</a>");
    return $response;
});

出力:
スクリーンショット 2019-06-16 14.23.15.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");
        });
    });
});

出力:
スクリーンショット 2019-06-16 14.46.19.png

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0