1. Go to this page and download the library: Download adeptoas/slim3-init library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
/** @var $app \Adepto\Slim3Init\SlimInit */
$app->addExceptionCallback(function(\Adepto\Slim3Init\Request $request, Throwable $t) {
// Send $t to the service
});
class ExceptionCallback {
public function __invoke(\Adepto\Slim3Init\Request $request, Throwable $t) {
// Send $t to the service
}
}
/** @var $app \Adepto\Slim3Init\SlimInit */
$app->addExceptionCallback(new ExceptionCallback());
/* Slim3 */
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
class YourMiddleware {
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Callable $next): ResponseInterface {
// Something before others run
$newResponse = $next($request, $response);
// Code after others have run
return $newResponse;
}
}
/* Slim4 */
use Psr\Container\ContainerInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Adepto\Slim3Init\Request;
class YourMiddleware {
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function __invoke(Request $request, RequestHandlerInterface $handler): ResponseInterface {
// Something before others run
$response = $handler->handle($request);
// Code after others have run
return $response;
}
}
use Adepto\Slim3Init\SlimInit;
use Adepto\Slim3Init\Request;
use Adepto\Slim3Init\Handlers\ExceptionHandler;
use Psr\Http\Message\ResponseInterface;
class NotFoundHandler extends ExceptionHandler {
public function handle(Request $request, Throwable $t, bool $displayDetails): ResponseInterface {
return $this->createResponse(404)
->withJson(['error' => 'not_found']);
}
}
// … your $app definition
/** @var $app SlimInit */
$app->setException(SomethingNotFoundException::class, NotFoundHandler::class);
use Adepto\Slim3Init\Exceptions\InternalErrorException;
use Adepto\Slim3Init\Exceptions\MethodNotAllowedException;
use Adepto\Slim3Init\Exceptions\NotFoundException;
use Adepto\Slim3Init\SlimInit;
/** @var $app SlimInit */
// Customize 404
$app->setException(NotFoundException::class, CustomHandler::class);
// Customize 405
$app->setException(MethodNotAllowedException::class, CustomHandler::class);
// Customize default handler (500)
$app->setException(InternalErrorException::class, CustomHandler::class);
// Customize all at once
$app->setException([
NotFoundException::class,
MethodNotAllowedException::class,
InternalErrorException::class
], CustomHandler::class);
use Adepto\Slim3Init\SlimInit;
// … your $app definition
/** @var $app SlimInit */
$app->setException(SomethingNotFoundException::class, 404);
/** @var $container \Adepto\Slim3Init\Container */
// Get value like normal, with exception if key was not found
$value = $container->get('some-value');
// Get value array-style, with null being returned if key was not found
$value = $container['some-value'];
use Adepto\Slim3Init\Middleware\Middleware;
use Adepto\Slim3Init\Request;
use Adepto\Slim3Init\Response;
use Psr\Http\Server\RequestHandlerInterface;
class YourMiddleware extends Middleware {
public function __invoke(Request $request, RequestHandlerInterface $handler) : Response{
return $this->createResponse(404);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.