Skip to content

Commit 6f7d60c

Browse files
Fix/Feature:
- allow ImplicitRouteBinding to match compound name ('foo_bar') route parameters to method/function naming ('fooBar')
1 parent b5e8c73 commit 6f7d60c

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/Illuminate/Routing/ImplicitRouteBinding.php

+13-9
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@ public static function resolveForRoute($container, $route)
2020
foreach ($route->signatureParameters(Model::class) as $parameter) {
2121
$class = $parameter->getClass();
2222

23-
if (array_key_exists($parameter->name, $parameters) &&
24-
! $route->parameter($parameter->name) instanceof Model) {
25-
$method = $parameter->isDefaultValueAvailable() ? 'first' : 'firstOrFail';
26-
23+
if (! $route->parameter($parameter->name) instanceof Model) {
2724
$model = $container->make($class->name);
2825

29-
$route->setParameter(
30-
$parameter->name, $model->where(
31-
$model->getRouteKeyName(), $parameters[$parameter->name]
32-
)->{$method}()
33-
);
26+
$parameterName = array_key_exists($parameter->name, $parameters) ? $parameter->name : null;
27+
28+
// check if parameter name used was camelized in routed callback method
29+
if (!$parameterName) {
30+
$snakeParamName = snake_case($parameter->name);
31+
$parameterName = array_key_exists($snakeParamName, $parameters) ? $snakeParamName : null;
32+
}
33+
34+
if ($parameterName) {
35+
$value = $model->where($model->getRouteKeyName(), $parameters[$parameterName])->firstOrFail();
36+
$route->setParameter($parameterName, $value);
37+
}
3438
}
3539
}
3640
}

tests/Routing/RoutingRouteTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,13 @@ public function testModelBindingWithBindingClosure()
700700
$this->assertEquals('tayloralt', $router->dispatch(Request::create('foo/TAYLOR', 'GET'))->getContent());
701701
}
702702

703+
public function testModelBindingWithCompoundParameterName()
704+
{
705+
$router = $this->getRouter();
706+
$router->resource('foo-bar', 'Illuminate\Tests\Routing\RouteTestResourceControllerWithModelParameter', ['middleware' => SubstituteBindings::class]);
707+
$this->assertEquals('12345', $router->dispatch(Request::create('foo-bar/12345', 'GET'))->getContent());
708+
}
709+
703710
public function testModelBindingThroughIOC()
704711
{
705712
$container = new Container;
@@ -1275,6 +1282,14 @@ public function returnParameter($bar = '')
12751282
}
12761283
}
12771284

1285+
class RouteTestResourceControllerWithModelParameter extends Controller
1286+
{
1287+
public function show(RoutingTestUserModel $fooBar)
1288+
{
1289+
return $fooBar->value;
1290+
}
1291+
}
1292+
12781293
class RouteTestClosureMiddlewareController extends Controller
12791294
{
12801295
public function __construct()

0 commit comments

Comments
 (0)