Skip to content

Commit

Permalink
[9.x] Use route parameters in view (#42461)
Browse files Browse the repository at this point in the history
* wip

* fix styles

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
yoeriboven and taylorotwell authored May 20, 2022
1 parent c73253e commit 7446089
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/Illuminate/Routing/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,29 @@ public function __construct(ResponseFactory $response)
*/
public function __invoke(...$args)
{
[$view, $data, $status, $headers] = array_slice($args, -4);
$routeParameters = array_filter($args, function ($key) {
return ! in_array($key, ['view', 'data', 'status', 'headers']);
}, ARRAY_FILTER_USE_KEY);

return $this->response->view($view, $data, $status, $headers);
$args['data'] = array_merge($args['data'], $routeParameters);

return $this->response->view(
$args['view'],
$args['data'],
$args['status'],
$args['headers']
);
}

/**
* Execute an action on the controller.
*
* @param string $method
* @param array $parameters
* @return \Symfony\Component\HttpFoundation\Response
*/
public function callAction($method, $parameters)
{
return $this->{$method}(...$parameters);
}
}
3 changes: 3 additions & 0 deletions tests/Integration/Routing/RouteViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public function testRouteViewWithParams()

$this->assertStringContainsString('Test bar', $this->get('/route/value1/value2')->getContent());
$this->assertStringContainsString('Test bar', $this->get('/route/value1')->getContent());

$this->assertEquals('value1', $this->get('/route/value1/value2')->viewData('param'));
$this->assertEquals('value2', $this->get('/route/value1/value2')->viewData('param2'));
}

public function testRouteViewWithStatus()
Expand Down

0 comments on commit 7446089

Please sign in to comment.