Skip to content

Commit 8c5292e

Browse files
authored
[8.x] Respect custom route key with explicit route model binding (#36375)
* feat: respect route custom key for explicit route model bindings * tests: close mockery after test * style: changes from StyleCI
1 parent aa19ecd commit 8c5292e

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/Illuminate/Routing/RouteBinding.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public static function forCallback($container, $binder)
3333
*/
3434
protected static function createClassBinding($container, $binding)
3535
{
36-
return function ($value, $route) use ($container, $binding) {
36+
return function ($value, $route, $key) use ($container, $binding) {
3737
// If the binding has an @ sign, we will assume it's being used to delimit
3838
// the class name from the bind method name. This allows for bindings
3939
// to run multiple bind methods in a single class for convenience.
4040
[$class, $method] = Str::parseCallback($binding, 'bind');
4141

4242
$callable = [$container->make($class), $method];
4343

44-
return $callable($value, $route);
44+
return $callable($value, $route, $key);
4545
};
4646
}
4747

@@ -57,7 +57,7 @@ protected static function createClassBinding($container, $binding)
5757
*/
5858
public static function forModel($container, $class, $callback = null)
5959
{
60-
return function ($value) use ($container, $class, $callback) {
60+
return function ($value, $route, $key) use ($container, $class, $callback) {
6161
if (is_null($value)) {
6262
return;
6363
}
@@ -67,7 +67,7 @@ public static function forModel($container, $class, $callback = null)
6767
// throw a not found exception otherwise we will return the instance.
6868
$instance = $container->make($class);
6969

70-
if ($model = $instance->resolveRouteBinding($value)) {
70+
if ($model = $instance->resolveRouteBinding($value, $route->bindingFieldFor($key))) {
7171
return $model;
7272
}
7373

src/Illuminate/Routing/Router.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ public function substituteImplicitBindings($route)
840840
*/
841841
protected function performBinding($key, $value, $route)
842842
{
843-
return call_user_func($this->binders[$key], $value, $route);
843+
return call_user_func($this->binders[$key], $value, $route, $key);
844844
}
845845

846846
/**

tests/Routing/RoutingRouteTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Illuminate\Routing\UrlGenerator;
3030
use Illuminate\Support\Str;
3131
use LogicException;
32+
use Mockery;
3233
use PHPUnit\Framework\TestCase;
3334
use stdClass;
3435
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
@@ -938,6 +939,36 @@ public function testModelBinding()
938939
$this->assertSame('TAYLOR', $router->dispatch(Request::create('foo/taylor', 'GET'))->getContent());
939940
}
940941

942+
public function testModelBindingWithCustomKey()
943+
{
944+
// Create the router.
945+
$container = new Container();
946+
$router = new Router(new Dispatcher(), $container);
947+
$container->singleton(Registrar::class, function () use ($router) {
948+
return $router;
949+
});
950+
951+
$router->get('foo/{bar:custom}', ['middleware' => SubstituteBindings::class, 'uses' => function ($name) {
952+
return $name;
953+
}]);
954+
$router->model('bar', RouteModelBindingStub::class);
955+
956+
// Mock the stub so we can verify that the method is called with custom key.
957+
$mock = $container->instance(
958+
RouteModelBindingStub::class,
959+
Mockery::mock(RouteModelBindingStub::class),
960+
);
961+
962+
$mock->shouldReceive('resolveRouteBinding')
963+
->with('taylor', 'custom')
964+
->once()
965+
->andReturn('TAYLOR');
966+
967+
$this->assertSame('TAYLOR', $router->dispatch(Request::create('foo/taylor', 'GET'))->getContent());
968+
969+
Mockery::close();
970+
}
971+
941972
public function testModelBindingWithNullReturn()
942973
{
943974
$this->expectException(ModelNotFoundException::class);

0 commit comments

Comments
 (0)