Ground Sunlight

Windowsで作る - PHPプログラミングの開発環境

ユーザ用ツール

サイト用ツール


サイドバー

メインメニュー

XAMPP アレンジ

IED

WSL2

道具箱

リポジトリ編

フレームワーク編

公開ソフトウェア

メタ
リンク


このページへのアクセス
今日: 2 / 昨日: 5
総計: 2238

slim:4:cookbook

文書の過去の版を表示しています。


Slim4 クックブック

Version 4.5.0

y2sunlight 2020-09-23

Slim に戻る

関連記事

本章は以下のサイトの Cook book のセクションを翻訳し若干の補足を加えたのもです。


Trailing / in route patterns

Slim treats a URL pattern with a trailing slash as different to one without. That is, /user and /user/ are different and so can have different callbacks attached.

For GET requests a permanent redirect is fine, but for other request methods like POST or PUT the browser will send the second request with the GET method. To avoid this you simply need to remove the trailing slash and pass the manipulated url to the next middleware.

If you want to redirect/rewrite all URLs that end in a / to the non-trailing / equivalent, then you can add this middleware:

Slimは、末尾にスラッシュがあるURLパターンを、ないものとは異なるものとして扱います。 つまり、/user/user/ は異なるため、異なるコールバックをアタッチできます。

GETリクエストの場合、永続的なリダイレクトは問題ありませんが、POSTやPUTなどの他のリクエストメソッドの場合、ブラウザはGETメソッドを使用して2番目のリクエストを送信します。 これを回避するには、末尾のスラッシュを削除し、操作されたURLを次のミドルウェアに渡す必要があります。

/ で終わるすべてのURLを、末尾以外の / に相当するものにリダイレクト/書き換える場合は、次のミドルウェアを追加できます。

<?php
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface RequestHandler;
use Slim\Factory\AppFactory;
use Slim\Psr7\Response;
 
require __DIR__ . '/../vendor/autoload.php';
 
$app = AppFactory::create();
 
$app->add(function (Request $request, RequestHandler $handler) {
    $uri = $request->getUri();
    $path = $uri->getPath();
 
    if ($path != '/' && substr($path, -1) == '/') {
        // recursively remove slashes when its more than 1 slash
        $path = rtrim($path, '/');
 
        // permanently redirect paths with a trailing slash
        // to their non-trailing counterpart
        $uri = $uri->withPath($path);
 
        if ($request->getMethod() == 'GET') {
            $response = new Response();
            return $response
                ->withHeader('Location', (string) $uri)
                ->withStatus(301);
        } else {
            $request = $request->withUri($uri);
        }
    }
 
    return $handler->handle($request);
});

Alternatively, consider middlewares/trailing-slash middleware which also allows you to force a trailing slash to be appended to all URLs:

または、middlewares/trailing-slash ミドルウェアを検討してください。これにより、すべてのURLに末尾のスラッシュを強制的に追加することもできます。

use Middlewares\TrailingSlash;
 
$app->add(new TrailingSlash(true)); // true adds the trailing slash (false removes it)


Retrieving Current Route

If you ever need to get access to the current route within your application, you will need to instantiate the RouteContext object using the incoming ServerRequestInterface.

アプリケーション内の現在のルートにアクセスする必要がある場合は、着信時の ServerRequestInterface を使用して RouteContext オブジェクトをインスタンス化する必要があります。

From there you can get the route via $routeContext→getRoute() and access the route’s name by using getName() or get the methods supported by this route via getMethods(), etc.

そこから、$routeContext→getRoute() を介してルートを取得し、getName() を使用してルートの名前にアクセスするか、getMethods() などを介してこのルートでサポートされているメソッドを取得できます。

Note: If you need to access the RouteContext object during the middleware cycle before reaching the route handler you will need to add the RoutingMiddleware as the outermost middleware before the error handling middleware (See example below).

注:ルートハンドラーに到達する前にミドルウェアサイクル中に RouteContext オブジェクトにアクセスする必要がある場合は、エラー処理ミドルウェアの前に、RoutingMiddleware を最も外側のミドルウェアとして追加する必要があります(以下の例を参照)。

Example:

例:

<?php
use Slim\Exception\HttpNotFoundException;
use Slim\Factory\AppFactory;
use Slim\Routing\RouteContext;
 
require __DIR__ . '/../vendor/autoload.php';
 
$app = AppFactory::create();
 
// Via this middleware you could access the route and routing results from the resolved route
$app->add(function (Request $request, RequestHandler $handler) {
    $routeContext = RouteContext::fromRequest($request);
    $route = $routeContext->getRoute();
 
    // return NotFound for non existent route
    if (empty($route)) {
        throw new HttpNotFoundException($request);
    }
 
    $name = $route->getName();
    $groups = $route->getGroups();
    $methods = $route->getMethods();
    $arguments = $route->getArguments();
 
    // ... do something with the data ...
 
    return $handler->handle($request);
});
 
// The RoutingMiddleware should be added after our CORS middleware so routing is performed first
$app->addRoutingMiddleware();
 
// The ErrorMiddleware should always be the outermost middleware
$app->addErrorMiddleware(true, true, true);
 
// ...
 
$app->run();


Setting up CORS

CORS - Cross origin resource sharing

A good flowchart for implementing CORS support Reference:

CORS server flowchart

You can test your CORS Support here: http://www.test-cors.org/

You can read the specification here: https://www.w3.org/TR/cors/

CORS-クロスオリジンリソースシェアリング

CORSサポートを実装するための適切なフローチャートリファレンス:

CORSサーバーのフローチャート

CORSサポートはここでテストできます:http:www.test-cors.org/ ここで仕様を読むことができます:https:www.w3.org/TR/cors/

The simple solution

 
 
 


Access-Control-Allow-Methods

 


Access-Control-Allow-Credentials

 


Uploading files using POST forms

 
 


コメント

コメントを入力. Wiki文法が有効です:
 
slim/4/cookbook.1602207001.txt.gz · 最終更新: 2020/10/09 10:30 by y2sunlight