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 Beta Release Feedback #2697
Comments
follow #2653 (comment) <?php
use Slim\Http\Decorators\ServerRequestDecorator;
use Slim\Factory\ServerRequestCreatorFactory;
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Interfaces\RouteCollectorProxyInterface as RouteCollectorProxy;
$container = new Container();
AppFactory::setContainer($container);
$app = AppFactory::create();
$app->group('/user', function (RouteCollectorProxy $group) {
$group->put('/ticket/{id}', UserController::class . ':ticket_update');
}
$serverRequestCreator = ServerRequestCreatorFactory::create();
$request = $serverRequestCreator->createServerRequestFromGlobals();
$request = new ServerRequestDecorator($request);
$response = $app->handle($request);
class UserController
{
public function update($request, $response, $args)
{
$content = $request->getParam('content');
var_dump($content);
exit();
}
} My posted form data is
But I just got a NULL in response. |
@SakuraSa233 The code |
@SakuraSa233 first of all you don’t need to do any of this boilerplate code anymore. Update to Also please make sure you’re using the latest version of Slim-Http as there have been some recent changes. From the code I see, the decoration should work though and getParam() should work but not for the purpose you’re intending to use. As @RyanNerd pointed out you need to use |
I tried it on |
@SakuraSa233 in order for the body to be parsed it needs to be a Your incoming request should look like this POST /user/ticket/1 HTTP/1.1
Content-Type: application/x-www-form-urlencoded
X-Http-Method-Override: PUT Add the method override middleware to your app <?php
use DI\Container;
use Slim\Factory\AppFactory;
use Slim\Interfaces\RouteCollectorProxyInterface as RouteCollectorProxy;
use Slim\Middleware\MethodOverrideMiddleware;
$container = new Container();
AppFactory::setContainer($container);
// Create App and add MethodOverrideMiddleware
$app = AppFactory::create();
$app->add(MethodOverrideMiddleware::class);
// Add Routes
$app->group('/user', function (RouteCollectorProxy $group) {
$group->put('/ticket/{id}', UserController::class . ':ticket_update');
}
$app->run(); |
Oh!!! That should be this... |
I think the current behaviour is not correct. The Example Slim 3 code: require __DIR__ . "/vendor/autoload.php";
$app = new \Slim\App;
$app->put("/test", function ($request, $response) {
var_dump($request->getParsedBody());
});
$app->run(); yields:
Similar Slim 4 version: use Slim\Factory\AppFactory;
use Slim\Middleware\RoutingMiddleware;
use Slim\Middleware\MethodOverrideMiddleware;
require __DIR__ . "/../vendor/autoload.php";
$app = AppFactory::create();
$routeResolver = $app->getRouteResolver();
$routingMiddleware = new RoutingMiddleware($routeResolver);
$app->add($routingMiddleware);
$methodOverrideMiddleware= new MethodOverrideMiddleware;
$app->add($methodOverrideMiddleware);
$app->put("/test", function($request, $response, $args) {
var_dump($request->getParsedBody());
return $response;
});
$app->run(); yields:
You can get some output by mimicking an old client which does not support
|
@tuupola there’s no media parsers in Slim-Psr7. It doesn’t handle JSON by default. As seen in your second last example you’re using Edit: |
@tuupola see this thread to implement an |
@l0gicgate Ah of course. Misunderstood the reason why |
Just a quick note // error
require __DIR__ . '/vendor/autoload.php.'; // there is a trailing dot after .php
// should be
require __DIR__ . '/vendor/autoload.php'; // no trailing dot after .php |
@merlindiavova Nice catch. Do you want to make a pull request? |
@adriansuter made a pull request. |
I notice a lack of silent flag on Additionally, there is an instantiation of a fixed class within the run method. As i prefer testing the whole request, this prevents me from having more than one tests (headers already sent).
Should i just build a PR to re-enable this feature, or is this by design and there is another way to test in this fashion? best regards, EDIT answering my own question: just use |
There is some mistake. The HTTP 200 status is returned with the middleware of the example and the HTTP 404 return code Sample code $app->add(function (Request $request, RequestHandler $handler) {
$response = $handler->handle($request);
$existingContent = (string) $response->getBody();
$response = new Response();
$response->getBody()->write('BEFORE-' . $existingContent);
return $response;
});
$app->add(function (Request $request, RequestHandler $handler) {
$response = $handler->handle($request);
$response->getBody()->write('AFTER ');
return $response;
});
$app->get('/', function (Request $request, Response $response, $args) {
$response->getBody()->write('-Hello World -');
//throw new Slim\Exception\HttpNotFoundException($request, "Product not found..."); // Does not work returns 200
return $response->withStatus(404); // <<< Does not work returns 200
//return $response;
});
$app->run(); |
@http200it There is something bad with your snippet. I try it simulate on my local env and don't know what is your RequestHandler doing there, |
The 500 status return has been added, however this is ignored and is returned 200. Step 4: Hello World >> http://slim-website.lgse.com/docs/v4/start/installation.html Example code :
In the $app->get('/' method, I intentionally return 500 and I think it should work. |
@http200it OK, now I get it where is the problem. Its your first registered middleware
Because Slim 4 using LIFO (Last In First Out) so your first registered middleware (up there), will be executed last and there you creating
It will work fine then. |
I understand and thank you for the explanation. |
I just upgraded a small demo app from Slim 3 to this beta. Everything went fairly painlessly, following the upgrade guide. I was already using the PHP-DI bridge, which now seems no longer needed. Biggest gotcha I ran into was this one: zendframework/zend-diactoros#278 I prefer extending |
@Brammm why couldn't you use the |
@l0gicgate This is my setup: https://github.com/Brammm/ProophSampleApp/blob/master/src/Api/Application.php I did end up creating a simple middleware to do the content parsing. |
@Brammm I see you're extending the |
I can take a look at making a PR for that middleware.
…On Tue, 18 Jun 2019, 20:01 Pierre Bérubé, ***@***.***> wrote:
@Brammm <https://github.com/Brammm> I see you're extending the App. I
mean that's totally fine but then you can't leverage the automatic
detection mechanism unfortunately. And I think that it might be a good idea
for us to look into implementing some sort of content parsing middleware.
It would probably be a great addition to the middleware collection we
already ship.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2697?email_source=notifications&email_token=AAGP35PCB5PABIKDQRZRGIDP3EPHRA5CNFSM4HN3T2R2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODX7O2UI#issuecomment-503246161>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAGP35NVYPSYP53WQIG26MLP3EPHRANCNFSM4HN3T2RQ>
.
|
@l0gicgate Using Slim v4. Reading docs: http://dev.slimframework.com/docs/v4/objects/request.html#route-object Using Slim's PSR-7 implementation: https://github.com/slimphp/Slim-Psr7 Playing around with
But in Slim's PSR-7 implementation in Slim v3 has Will this method be added back? Or it was intentionally removed for some reasons? |
@scsmash3r Slim PSR-7 follows strictly the PSR-7 definitions. You need to use a decorator like https://github.com/slimphp/Slim-Http to be able to access methods like |
I'm wondering if it would be better to put the Twig view into the container using its FQCN instead of just It could help with auto-wiring containers, for example: use Slim\Views\Twig;
// Auto-wiring can work based on the type hint.
public function __construct(Twig $view) {} Also, when using annotations currently: /**
* @Inject("view")
* @var Twig
*/
protected $view; Could be: /**
* @Inject
* @var Twig
*/
protected $view; Or when PHP 7.4 is released: /** @Inject */
protected View $view;
|
There is an optional parameter called |
@adriansuter Oh, nice. That change hasn't been released yet though. 🙂 |
Not yet. And by the way, I totally agree. To add another reason to your list: my IDE understands the concept of containers and if I write |
Closing this thread follow in #2770 |
Slim 4 Beta Release
See the full release notes. You may also look at the Alpha release notes for more details about this major release.
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 and test the 4.0.0-beta 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-beta
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
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: