Slim 은 URL 패턴 끝에 슬래시가 있는 것과 없는 것을 다르게 처리한다.
/user 와 /user/ 는 다른 콜백을 가질 수 있다.
슬래시로 끝나지 않는 모든 URL 에 슬래시를 붙여 리다이렉트 하려면 다음과 같이 미들웨어를 추가한다:
use Psr\Http\Message\RequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; $app->add(function (Request $request, Response $response, callable $next) { $uri = $request->getUri(); $path = $uri->getPath(); if ($path != '/' && substr($path, -1) == '/') { // permanently redirect paths with a trailing slash // to their non-trailing counterpart $uri = $uri->withPath(substr($path, 0, -1)); return $response->withRedirect((string)$uri, 301); } return $next($request, $response); }); | cs |
다른 방법으로, 모든 URL 의 끝에 강제로 슬래시를 붙이기 위해 oscarotero/psr7-middlewares 의 TrailingSlash 미들웨어를 사용할 수 있다.
use Psr7Middlewares\Middleware\TrailingSlash; $app->add(new TrailingSlash(true)); // true adds the trailing slash (false removes it) | cs |
WRITTEN BY
- 손가락귀신
정신 못차리면, 벌 받는다.
,