dentro de la carpeta de nuestro proyecto,
agregar la dependencia de slim
sudo composer require slim/slim:3.*
agregar la dependencia de monolog
sudo composer require monolog/monolog
----------------------------------------------------
nota: podemos levantar el servidor integrado de php, así:
php -S localhost:8000 -t public/
(siempre que los archivos esten en la carpeta public)
------------------------------------------
en el archivo public/index.php
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Utils\NumberConversionUtils;
use Utils\TestUtils;
require '../vendor/autoload.php';
$configuration = [
'settings' => [
'displayErrorDetails' => true,
],
];
$container = new \Slim\Container($configuration);
$app = new \Slim\App($container);
$container['Logger'] = function($c) {
$logger = new Monolog\Logger('my_logger');
$filename = _DIR__ . '/../log/error.log';
$file_handler = new Monolog\Handler\StreamHandler($filename);
$logger->pushHandler($file_handler);
return $logger;
};
$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
$this->get('Logger')->addInfo('Something interesting happened');
return $response;
});
$app->run();
este es un buen ejemplo, gracias
ResponderEliminar