Last active
September 1, 2019 16:03
-
-
Save fahmiegerton/695679e1d8d4184978e30af1067a8925 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use DI\ContainerBuilder; | |
use Monolog\Handler\StreamHandler; | |
use Monolog\Logger; | |
use Monolog\Processor\UidProcessor; | |
use Psr\Container\ContainerInterface; | |
use Psr\Log\LoggerInterface; | |
return function (ContainerBuilder $containerBuilder) { | |
$containerBuilder->addDefinitions([ | |
LoggerInterface::class => function (ContainerInterface $c) { | |
$settings = $c->get('settings'); | |
$loggerSettings = $settings['logger']; | |
$logger = new Logger($loggerSettings['name']); | |
$processor = new UidProcessor(); | |
$logger->pushProcessor($processor); | |
$handler = new StreamHandler($loggerSettings['path'], $loggerSettings['level']); | |
$logger->pushHandler($handler); | |
return $logger; | |
}, | |
// database | |
PDO::class => function (ContainerInterface $c) { | |
$settings = $c->get('settings')['db']; | |
$server = $settings['driver'].":host=".$settings['host'].";dbname=".$settings['dbname']; | |
$conn = new PDO($server, $settings['user'], $settings['pass'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); | |
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | |
return $conn; | |
} | |
]); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use App\Application\Actions\User\ListUsersAction; | |
use App\Application\Actions\User\ViewUserAction; | |
use Psr\Http\Message\ResponseInterface as Response; | |
use Psr\Http\Message\ServerRequestInterface as Request; | |
use Slim\App; | |
use Slim\Interfaces\RouteCollectorProxyInterface as Group; | |
return function (App $app) { | |
$container = $app->getContainer(); | |
$app->get('/', function (Request $request, Response $response) { | |
$response->getBody()->write('Hello world!'); | |
return $response; | |
}); | |
$app->group('/users', function (Group $group) use ($container) { | |
$group->get('', ListUsersAction::class); | |
$group->get('/{id}', ViewUserAction::class); | |
}); | |
$app->get('/buku', function (Request $request, Response $response) { | |
$sql = 'SELECT * FROM buku'; | |
$db = $this->get(PDO::class); | |
$stmt = $db->prepare($sql); | |
$stmt->execute(); | |
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
$payload = json_encode([ | |
'status' => 'success', | |
'data' => $result | |
]); | |
$response->getBody()->write($payload); | |
return $response | |
->withHeader('Content-Type', 'application/json') | |
->withStatus(201); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
use DI\ContainerBuilder; | |
use Monolog\Logger; | |
return function (ContainerBuilder $containerBuilder) { | |
// Global Settings Object | |
$containerBuilder->addDefinitions([ | |
'settings' => [ | |
'displayErrorDetails' => true, // Should be set to false in production | |
'logger' => [ | |
'name' => 'slim-app', | |
'path' => isset($_ENV['docker']) ? 'php://stdout' : __DIR__ . '/../logs/app.log', | |
'level' => Logger::DEBUG, | |
], | |
'db' => [ | |
'host' => 'localhost', | |
'user' => 'root', | |
'pass' => '', | |
'dbname' => 'perpusceritanya', | |
'driver' => 'mysql' | |
], | |
], | |
]); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment