New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Slim 4 Alpha Release Feedback #2653
Comments
@l0gicgate Just a little nitpik: |
Good catch @RyanNerd! Fixed |
@l0gicgate Version 3 has a Tutorial link with a guide to creating a "first application". Slim 4 with all the decoupling and plug-in choices (PSR-7, Containers, Route & ErrorHandling as middleware, etc.) is much more complex than Slim 3. This can be overwhelming to someone new to Slim (and even those like myself that are familiar with version 3). |
@RyanNerd I removed the tutorial from the v4 docs for now. It does need to be written and will be added to the docs later. In the mean time someone can look at the skeleton repository to see how things are implemented in a "real life" scenario. I also think that a video tutorial would be much better. I just need time to get it done. |
|
composer require slim/psr7:dev-master it's not "psr-7" |
See if you can tell who didn't run it but commented anyway... 😆 |
@RyanNerd simply return a response object without calling |
It looks like |
@davidbyoung I documented on how to handle all errors in the docs under Notices & Warnings handling. This shows a more advanced way of handling all errors. See http://dev.slimframework.com/docs/v4/objects/application.html#notices-and-warnings-handling |
Note the "Edit this page" links in the documentation refer to something like: https://github.com/slimphp/Slim-Website/tree/gh-pages/docs/v4/objects/routing.md which does not exist, while https://github.com/slimphp/Slim-Website/tree/gh-pages-4.x/docs/v4/objects/routing.md Should I create an issue for this? Or is it expected "for now"... 😄 |
@holtkamp thanks for reporting this. It has to stay the same until I merge the |
In the documentation and the readme file, |
How about adding suggestions for the PSR-7 implementations mentioned in the
|
Hi, I have a question about default middleware implementation. But first I need to clear my understanding the idea abou using this framework. When I look at your setup of App, it seems like whole idea is setup app via DI, cause the factories etc., is that right? Second is about middleware, I get idea, like all is middleware so everything is implemented same way. But It seems like I need to setup everyting by myself, is it right conclusion? And at the end, wouldn't be better if basic middlewares, like RouteMiddleware and ErrorMiddleware has been setup automaticly? |
@bednic the App can run without a container. It sure makes one’s life a lot easier to provide one though.
As for |
Hello. Missing an example of using sessions with cookies |
@danielspk I'm not sure exactly how to show an example of that since Slim doesn't handle sessions. That's typically something you'd implement via middleware or if you want sessions turned on at all times just use |
I can't get the middleware examples to work. EDIT: I've figured out how it works, but the example remains incredibly confusing. As far as I can tell there's no way to write to the body in middleware before the route does its thing. |
Hi, I made a simple repository that uses travis-ci to automatically test the slim4 framework against multiple PSR-7 implementations. See https://github.com/adriansuter/Slim4-CI Currently, all are working fine! |
Awesome @adriansuter thank you. You should join our Slack so we can chat |
I'm having trouble with POST requests always returning null for Here's the code: <?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Slim\Middleware\ErrorMiddleware;
use Slim\Middleware\RoutingMiddleware;
require __DIR__ . '/vendor/autoload.php';
$app = AppFactory::create();
// Add Routing Middleware
$routeResolver = $app->getRouteResolver();
$routingMiddleware = new RoutingMiddleware($routeResolver);
$app->add($routingMiddleware);
// Error middleware
$callableResolver = $app->getCallableResolver();
$responseFactory = $app->getResponseFactory();
$errorMiddleware = new ErrorMiddleware($callableResolver, $responseFactory, true, true, true);
$app->add($errorMiddleware);
// Define app routes
$app->post('/posttest', function (Request $request, Response $response, $args) {
$content = $request->getParsedBody();
if ($content === null) {
$response->getBody()->write('Error no JSON in request body or something else?!?!?!?!?');
} else {
$response->getBody()->write('getParsedBody() actually worked');
}
return $response;
});
// Run app
$app->run(); |
@RyanNerd - parsedBody gets populated with $_POST, but in PHP you have to get JSON data from php://input |
@RyanNerd maybe related to this: slimphp/Slim-Psr7#38, which is fixed already. When doing your POST request, what headers are sent in the request? |
@Serdan The docs state differently :
|
@l0gicgate I think the docs need to be corrected. Not every HTTP request has a body (per the HTTP spec all requests MAY have a body), |
To get parsed, I think the ContentType header in the POST request should contains something like:
Also see this line https://github.com/slimphp/Slim-Psr7/blob/bd3737bc6fe94d4ba30febe3aa7bc25d3f07f6ca/src/Factory/ServerRequestFactory.php#L105 |
@RyanNerd I need to update the docs for sure. Slim PSR-7 does not parse the body like Slim 3 used to. All that functionality has been moved over to Slim-Http. However you can write simple middleware to parse the body and append it to the incoming In case of In order for actual form data to be parsed it needs to be a Note that this is very basic middleware and you could add more content types. <?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as RequestInterface;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
class JsonBodyParserMiddleware implements MiddlewareInterface {
public function process(Request $request, RequestHandler $handler): Response
{
$contentType = $request->getHeaderLine('Content-Type');
if (strstr($contentType, 'application/json')) {
$contents = json_decode(file_get_contents('php://input'));
if (json_last_error() !== JSON_ERROR_NONE) {
$request = $request->withParsedBody($contents);
}
}
return $handler->handle($request);
}
} In any case, I will update the docs to remove the outdated portion of the docs about parsing the body. |
Is there a release schedule of Slim's 4.x branch available? Until when is Slim's 3.x branch planned to be maintained? I'm about to start a new project and I'm hesitating between which branch to use for it. |
@magikstm I don't know the scope of your project but I've started a new project using 4.x alpha and other than one misunderstanding (on my part, and partly because the docs were unclear) I've not had any issues. |
Question: Is there a performance or other advantages to have a middleware class |
@magikstm the 4.x branch is pretty stable despite being in alpha. I'd go with Slim 4 if you're starting a new project. |
@RyanNerd no significant difference between the two. |
As @Serdan mentioned already, the example code in the documentation is not working as expected.
I just created an issue for that. slimphp/Slim-Website#356 |
I would like to ask Slim 4.x support Doctrine ORM? |
@engineer-log you can use Doctrine ORM, Eloquent, or any other persistence library alongside with Slim. |
I was thinking what probably most of you are dealing with and I presume that most scenario is building some API, probably REST. Whole back-end based on SlimPHP maybe with Doctrine, maybe with Eloquent. I'm currently working on library for the API part. It's implementation of JSON API ,but annotation based. So if you are using Doctrine, you get my idea rly quick. So if is someone interested, I would appreciate every idea or help. It's my first library, so i don't know how to get feedback. I don't know if this isn't too unpolite 🤔, advertise my library here. But I'm using combo Slim + Doctrine + API some time, and I think maybe it will help someone. But if this doesn't belong here I will delete it and of course I'm sorry. P.S.: Good work, this is most clean and straightforward framework I ever seen. I was searching for this kind of framework for a years! Thank you, all of you.👍 |
@bednic that's cool! I went through the README of your repo and I understand what you're doing. I'd like to see an example of a route with Slim 4 😄 you should put a skeleton repository together with Doctrine, Slim and your JSON API library! |
a bit off-topic (sorry), but @bednic: you check these libraries for usage with JSON-API by @kocsismate, very elegant and clean! |
That kind of library is the reason why I make my own. Almost in every library it's necessary to create some ResourceObject, then gather all attributes and relationships, pass them to object, or schema and then serialize object to valid JSON. My library map all attributes and relationships by annotations, same as Doctrine. I'm gathering most of needed informations by reflection. If you are writing code standardized like object with private props with getters and setters, then I don't need anything else and just make valid JSON-API document for you from your object(s). It's much less writing. |
HomeController: class HomeController
{
public function __construct()
{
$renderer = new \Slim\Views\PhpRenderer();
// other codes...
return $renderer;
}
public function index(Request $request, Response $response, $args): Response
{
return $this->renderer->render([$response], 'index.phtml');
}
// other functions...
}
Middleware: $callableResolver = $app->getCallableResolver();
$responseFactory = $app->getResponseFactory();
$errorMiddleware = new ErrorMiddleware($callableResolver, $responseFactory, false, false, false);
$handler = function ($request, $exception, $displayErrorDetails, $logErrors, $logErrorDetails) {
$code = $exception->getCode();
$response = new Slim\Psr7\Response();
switch ($code) {
case 404:
return $response->withHeader('Location', '/404')->withStatus(302);
case 405:
return $response->withHeader('Location', '/405')->withStatus(302);
case 500:
return $response->withHeader('Location', '/500')->withStatus(302);
}
};
$errorMiddleware->setDefaultErrorHandler($handler);
$app->add($errorMiddleware); Route: $app->get('/', \HomeController::class . ':index'); I expected to get a Slim 500 error with details and trace.
|
It might be problem with Eception::getCode(), maybe it's not Slim HttpException and therefore getCode might return 0, in which case return value from handler will be null. |
Yeah..You are right...Sorry for my bother. |
@SakuraSa233 hence why you should always handle default cases, this is a prime example of it. |
Here is another question is that my Slim 3 code always get public function update($request, $response, $args)
{
$data = $request->getParam('data');
// ...
} I have known that |
@SakuraSa233 do you have Slim-Http decorators installed? Also you may need to require the 4.x branch right now instead of the 4.0.0-alpha pre-release since the automatic detection for Slim-Http decorators isn’t in the 4.0.0-alpha release. |
Closing this. Follow thread in Slim 4 Beta Release Feedback Thread. |
There is some mistake. The HTTP 200 status is returned with the middleware of the example and the HTTP 404 return code `
}); $app->add(function (Request $request, RequestHandler $handler) { $app->get('/', function (Request $request, Response $response, $args) {
}); $app->run(); ` |
Slim 4 Alpha Release
See the full release notes
Before doing anything read the docs
I just finished the first draft of the docs for Slim 4 which are available here. I need feedback please:
http://dev.slimframework.com/docs/v4
Download an test the 4.0.0-alpha release
You may also play around with the
4.x
branch and create a simple app with it to test things out.composer require slim/slim:4.0.0-alpha
Install a PSR-7 Implementation
You will also need to install a PSR-7 implementation and ServerRequestCreator combo. You can use Slim's PSR-7 Implementation or choose one of the ones described on the
4.x
branch README: https://github.com/slimphp/Slim/blob/4.x/README.mdcomposer require slim/psr7:dev-master
Slim 4 DDD API Skeleton Prototype
I also just created a Slim4 Skeleton with a DDD style directory structure with example files and test coverage for everything. I'm not sure if it's the right fit yet as it may be a bit too opinionated but I'd love some feedback:
If you have any questions don't hesitate to ping me on Slack or ask questions in this thread directly, I'm available to help!
The text was updated successfully, but these errors were encountered: