Skip to content

Commit 173bc1c

Browse files
committed
added slim framework
1 parent c30c397 commit 173bc1c

File tree

5 files changed

+1616
-7
lines changed

5 files changed

+1616
-7
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Just a DEMO API built around SLIM framework and GraphQL-PHP
88

99
From the project main directory:
1010

11-
(on windows, use `winpty` before `docker exec ...`)
11+
(on windows, you may want to use `winpty` before `docker exec ...`)
1212

1313

1414
```bash
@@ -22,3 +22,11 @@ Connect to container as CLI (optional)
2222
```bash
2323
docker exec -ti gql_slim_api sh
2424
```
25+
26+
27+
## GraphQL
28+
29+
### Endpoints
30+
31+
* `/graphql` The main api endpoint.
32+
* `/graphql/refs` Another api endpoint with another schema

app/public/index.php

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,85 @@
11
<?php
22

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();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
use Psr\Container\ContainerInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
9+
class GraphqlController
10+
{
11+
private $container;
12+
13+
public function __construct(ContainerInterface $container)
14+
{
15+
$this->container = $container;
16+
}
17+
18+
public function mainEndpoint(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
19+
{
20+
// your code to access items in the container... $this->container->get('');
21+
22+
23+
return $response;
24+
}
25+
26+
public function refsEndpoint(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
27+
{
28+
// your code to access items in the container... $this->container->get('');
29+
30+
31+
return $response;
32+
}
33+
}

composer.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
{
22
"name": "rsouverain/php.demo.api.graphql.slim",
33
"type": "project",
4-
"require": {}
4+
"autoload": {
5+
"psr-4": {
6+
"App\\": "app/src/"
7+
}
8+
},
9+
"require": {
10+
"slim/slim": "4.*",
11+
"slim/psr7": "^1.3",
12+
"php-di/slim-bridge": "^3.1",
13+
"webonyx/graphql-php": "^14.4",
14+
"doctrine/dbal": "^3.0"
15+
}
516
}

0 commit comments

Comments
 (0)