Skip to content
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

$app->redirect() crashes when using DI\Container #2815

Closed
slokhorst opened this issue Aug 20, 2019 · 2 comments
Closed

$app->redirect() crashes when using DI\Container #2815

slokhorst opened this issue Aug 20, 2019 · 2 comments

Comments

@slokhorst
Copy link

slokhorst commented Aug 20, 2019

The following example causes a crash when visiting /test:

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

/* CAUSES CRASH */
$container = new DI\Container();
AppFactory::setContainer($container);
/* CAUSES CRASH */

$app = AppFactory::create();
$app->get('/', function (Request $request, Response $response, array $args) {
    $response->getBody()->write('Hello World');
    return $response;
});

$app->redirect('/test', '/');
$app->run();

Crash message:

Details
Type: Error
Code: 0
Message: Call to a member function createResponse() on null
File: vendor/slim/slim/Slim/Routing/RouteCollectorProxy.php
Line: 199
Trace
#0 vendor/slim/slim/Slim/Handlers/Strategies/RequestResponse.php(42): Slim\Routing\RouteCollectorProxy->Slim\Routing\{closure}(Object(Slim\Http\ServerRequest), Object(Slim\Http\Response), Array)
#1 vendor/slim/slim/Slim/Routing/Route.php(374): Slim\Handlers\Strategies\RequestResponse->__invoke(Object(Closure), Object(Slim\Http\ServerRequest), Object(Slim\Http\Response), Array)
#2 vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): Slim\Routing\Route->handle(Object(Slim\Http\ServerRequest))
#3 vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest))
#4 vendor/slim/slim/Slim/Routing/Route.php(339): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest))
#5 vendor/slim/slim/Slim/Routing/RouteRunner.php(65): Slim\Routing\Route->run(Object(Slim\Http\ServerRequest))
#6 vendor/slim/slim/Slim/Middleware/ErrorMiddleware.php(89): Slim\Routing\RouteRunner->handle(Object(Slim\Http\ServerRequest))
#7 vendor/slim/slim/Slim/MiddlewareDispatcher.php(124): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Http\ServerRequest), Object(Slim\Routing\RouteRunner))
#8 vendor/slim/slim/Slim/MiddlewareDispatcher.php(65): class@anonymous->handle(Object(Slim\Http\ServerRequest))
#9 vendor/slim/slim/Slim/App.php(174): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest))
#10 vendor/slim/slim/Slim/App.php(158): Slim\App->handle(Object(Slim\Http\ServerRequest))
#11 public_html/index.php(23): Slim\App->run()
#12 {main}

If you remove the bit between /* CAUSES CRASH */ from my example, the crash goes away.

The following notice is in my error log:

Undefined property: DI\Container::$responseFactory in vendor/slim/slim/Slim/Routing/RouteCollectorProxy.php on line 199

Using slim/slim 4.1.0 and php-di/php-di 6.0.9.

@l0gicgate
Copy link
Member

Thanks for reporting this. So I figured out what's happening. It's because when a container is present this callable gets bound to the container when it's resolved. And as seen we're using $this->responseFactory() when we should just pass it in:
https://github.com/slimphp/Slim/blob/4.x/Slim/Routing/RouteCollectorProxy.php#L198-L201

I'm creating a patch for it right now

@slokhorst
Copy link
Author

Thanks for the quick fix! I can confirm that #2816 fixes the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants