|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -$now = new \DateTime('now', new \DateTimeZone('UTC')); |
4 |
| -$now->setTimezone(new \DateTimeZone('Europe/Paris')); |
5 |
| -echo "Hello World !! ".$now->format(\DateTimeInterface::ISO8601); |
| 3 | +require __DIR__ . '/../../vendor/autoload.php'; |
| 4 | + |
| 5 | +use DI\Container; |
| 6 | +use Psr\Http\Message\ResponseInterface as ResponseInterface; |
| 7 | +use Psr\Http\Message\ServerRequestInterface as Request; |
| 8 | +use Psr\Http\Server\RequestHandlerInterface as RequestHandler; |
| 9 | +use Slim\Factory\AppFactory; |
| 10 | +use Slim\Psr7\Response; |
| 11 | + |
| 12 | +// Create Container using PHP-DI |
| 13 | +$container = new Container(); |
| 14 | + |
| 15 | +// Set container to create App with on AppFactory |
| 16 | +AppFactory::setContainer($container); |
| 17 | + |
| 18 | +// Creating the app kernel |
| 19 | +$app = AppFactory::create(); |
| 20 | + |
| 21 | +// Add Routing Middleware |
| 22 | +$app->addRoutingMiddleware(); |
| 23 | + |
| 24 | +/** |
| 25 | + * Middleware to handle removing trailing slashes (/) in route URLs |
| 26 | + * @see https://www.slimframework.com/docs/v4/cookbook/route-patterns.html |
| 27 | + */ |
| 28 | +$app->add(function (Request $request, RequestHandler $handler) { |
| 29 | + $uri = $request->getUri(); |
| 30 | + $path = $uri->getPath(); |
| 31 | + |
| 32 | + if ($path != '/' && substr($path, -1) == '/') { |
| 33 | + // recursively remove slashes when its more than 1 slash |
| 34 | + $path = rtrim($path, '/'); |
| 35 | + |
| 36 | + // permanently redirect paths with a trailing slash |
| 37 | + // to their non-trailing counterpart |
| 38 | + $uri = $uri->withPath($path); |
| 39 | + |
| 40 | + if ($request->getMethod() == 'GET') { |
| 41 | + $response = new Response(); |
| 42 | + return $response |
| 43 | + ->withHeader('Location', (string) $uri) |
| 44 | + ->withStatus(301); |
| 45 | + } else { |
| 46 | + $request = $request->withUri($uri); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return $handler->handle($request); |
| 51 | +}); |
| 52 | + |
| 53 | +/** |
| 54 | + * The routing middleware should be added earlier than the ErrorMiddleware |
| 55 | + * Otherwise exceptions thrown from it will not be handled by the middleware |
| 56 | + * @see https://www.slimframework.com/docs/v4/middleware/error-handling.html |
| 57 | + * |
| 58 | + * @param bool $displayErrorDetails -> Should be set to false in production |
| 59 | + * @param bool $logErrors -> Parameter is passed to the default ErrorHandler |
| 60 | + * @param bool $logErrorDetails -> Display error details in error log |
| 61 | + * which can be replaced by a callable of your choice. |
| 62 | + * @param \Psr\Log\LoggerInterface $logger -> Optional PSR-3 logger to receive errors |
| 63 | + * |
| 64 | + * Note: This middleware should be added last. It will not handle any exceptions/errors |
| 65 | + * for middleware added after it. |
| 66 | + */ |
| 67 | +$errorMiddleware = $app->addErrorMiddleware(true, true, true, $logger); |
| 68 | + |
| 69 | +// Demo index route |
| 70 | +$app->redirect('/', '/hello', 301); |
| 71 | + |
| 72 | +// Demo 'hello world' route |
| 73 | +$app->any('/hello', function (Request $request, ResponseInterface $response, $args) { |
| 74 | + $nowUTC = new \DateTime('now', new \DateTimeZone('UTC')); |
| 75 | + $nowFR = clone($nowUTC); |
| 76 | + $nowFR->setTimezone(new \DateTimeZone('Europe/Paris')); |
| 77 | + $response->getBody()->write("Hello World !! current DateTime in France(ISO8601): ".$nowFR->format(\DateTimeInterface::ISO8601)); |
| 78 | + return $response; |
| 79 | +}); |
| 80 | + |
| 81 | +// Demo graphql endpoints |
| 82 | +$app->map(['GET', 'POST'], '/graphql', \App\Controller\GraphqlController::class.':mainEndpoint'); |
| 83 | +$app->map(['GET', 'POST'], '/graphql/refs', \App\Controller\GraphqlController::class.':refsEndpoint'); |
| 84 | + |
| 85 | +$app->run(); |
0 commit comments