Passing data withHeader

Hello

I want to make a redirect in my Controller:

return $response->withHeader('Location', 'dashboard'])->withStatus(302);

But I want to pass some data to the view. But something like this doesn’t work:

return $response->withHeader('Location', 'dashboard', [
      'foo' = $foo
    ])->withStatus(302);

Can someone please help me?

Does your ‘dashboard’ route have a name?

You can generate a URL for this named route with the application RouteParser’s urlFor() method.

$app->get('/dashboard', function ($request, $response, $args) {
    $response->getBody()->write("Hello dashboard!");
    return $response;
})->setName('dashboard');

Usage:

$routeParser = \Slim\Routing\RouteContext::fromRequest($request)->getRouteParser();

// Outputs "/dashboard?foo=bar"
$url = $routeParser->urlFor('dashboard', ['foo' => 'bar']);

return $response->withHeader('Location', $url)->withStatus(302);
1 Like