Slim4-notes: Difference between revisions
Jump to navigation
Jump to search
(Created page with "=== Beginning of a cheat sheet === Pulled from various sources.. [https://blog.programster.org/slim-4-cheatsheet slim4 cheatsheet] == Get parsed body == <pre> $request->getParsedBody() </pre> * Apparently need some middleware for this to work <pre> $app->addBodyParsingMiddleware(); </pre> == Returning a redirect == <pre> /** * Create a redirect response; * @param string $newLocation - where you wish to redirect the user. * @param Slim\Psr7\Response $response - the...") |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
Pulled from various sources.. | Pulled from various sources.. | ||
[https://blog.programster.org/slim-4-cheatsheet slim4 cheatsheet] | * [https://blog.programster.org/slim-4-cheatsheet slim4 cheatsheet] | ||
* [https://perangkatlunakku.com/en/slim4-tutorial-api-4-autentikasi-pada-middleware/ pada middleware for authentication] | |||
* [https://perangkatlunakku.com/en/slim4-tutorial-api-3-jwt-access-token/ Create JWT for auth use in Slim4] | |||
* [https://discourse.slimframework.com/t/slim-4-classes-and-configurations-in-middleware/4103 configure middleware] | |||
* [https://odan.github.io/slim4-skeleton/security.html slim-skeleton auth] | |||
*[https://steemit.com/utopian-io/@alfarisi94/how-to-create-middleware-in-slim-with-closure middleware with closure and class] | |||
== Get parsed body == | == Get parsed body == |
Latest revision as of 09:23, 28 July 2023
Beginning of a cheat sheet
Pulled from various sources..
- slim4 cheatsheet
- pada middleware for authentication
- Create JWT for auth use in Slim4
- configure middleware
- slim-skeleton auth
- middleware with closure and class
Get parsed body
$request->getParsedBody()
- Apparently need some middleware for this to work
$app->addBodyParsingMiddleware();
Returning a redirect
/** * Create a redirect response; * @param string $newLocation - where you wish to redirect the user. * @param Slim\Psr7\Response $response - the existing response to work with * @param bool $useTemporaryRedirect - whether this redirect is temporary, or browser should cache it. * @return Slim\Psr7\Response - the redirect response. */ function redirect( string $newLocation, bool $useTemporaryRedirect=true ) : Slim\Psr7\Response { $httpCode = ($useTemporaryRedirect) ? 302 : 301; $response = new \Slim\Psr7\Response($httpCode); $response = $response->withHeader('Location', $newLocation); return $response; }
Auth Middleware
Not going to bother copy of all the code, but look at the cheetsheet linked above close to the bottom of the page.
Adding a 404 or other err pages
This also looks like a middleware function as it gets called before actually hitting the routes
$app = Slim\Factory\AppFactory::create(); // register any custom middleware.... $app->addMiddleware(new SomeCustomMiddleware()); $app->addMiddleware(new AnotherCustomMiddleware()); // register the error middleware. This must be registered last so that it gets executed first. $errorMiddleware = $app->addErrorMiddleware( $displayErrorDetails = (ENVIRONMENT === "dev"), $logErrors=true, $logErrorDetails=true ); // Create your 404 handler... $errorMiddleware->setErrorHandler(\Slim\Exception\HttpNotFoundException::class, function ( \Psr\Http\Message\ServerRequestInterface $request, \Throwable $exception, bool $displayErrorDetails, bool $logErrors, bool $logErrorDetails ) { $response = new \Slim\Psr7\Response(); $body = new View404(); $view = new ViewCustom404Page(); $response->getBody()->write((string)$view); return $response->withStatus(404); }); // register routes/controllers here.... $app->run();