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

[8.x] Respect custom route key with explicit route model binding #36375

Merged
merged 3 commits into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Illuminate/Routing/RouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public static function forCallback($container, $binder)
*/
protected static function createClassBinding($container, $binding)
{
return function ($value, $route) use ($container, $binding) {
return function ($value, $route, $key) use ($container, $binding) {
// If the binding has an @ sign, we will assume it's being used to delimit
// the class name from the bind method name. This allows for bindings
// to run multiple bind methods in a single class for convenience.
[$class, $method] = Str::parseCallback($binding, 'bind');

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

return $callable($value, $route);
return $callable($value, $route, $key);
};
}

Expand All @@ -57,7 +57,7 @@ protected static function createClassBinding($container, $binding)
*/
public static function forModel($container, $class, $callback = null)
{
return function ($value) use ($container, $class, $callback) {
return function ($value, $route, $key) use ($container, $class, $callback) {
if (is_null($value)) {
return;
}
Expand All @@ -67,7 +67,7 @@ public static function forModel($container, $class, $callback = null)
// throw a not found exception otherwise we will return the instance.
$instance = $container->make($class);

if ($model = $instance->resolveRouteBinding($value)) {
if ($model = $instance->resolveRouteBinding($value, $route->bindingFieldFor($key))) {
return $model;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ public function substituteImplicitBindings($route)
*/
protected function performBinding($key, $value, $route)
{
return call_user_func($this->binders[$key], $value, $route);
return call_user_func($this->binders[$key], $value, $route, $key);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Str;
use LogicException;
use Mockery;
use PHPUnit\Framework\TestCase;
use stdClass;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
Expand Down Expand Up @@ -938,6 +939,36 @@ public function testModelBinding()
$this->assertSame('TAYLOR', $router->dispatch(Request::create('foo/taylor', 'GET'))->getContent());
}

public function testModelBindingWithCustomKey()
{
// Create the router.
$container = new Container();
$router = new Router(new Dispatcher(), $container);
$container->singleton(Registrar::class, function () use ($router) {
return $router;
});

$router->get('foo/{bar:custom}', ['middleware' => SubstituteBindings::class, 'uses' => function ($name) {
return $name;
}]);
$router->model('bar', RouteModelBindingStub::class);

// Mock the stub so we can verify that the method is called with custom key.
$mock = $container->instance(
RouteModelBindingStub::class,
Mockery::mock(RouteModelBindingStub::class),
);

$mock->shouldReceive('resolveRouteBinding')
->with('taylor', 'custom')
->once()
->andReturn('TAYLOR');

$this->assertSame('TAYLOR', $router->dispatch(Request::create('foo/taylor', 'GET'))->getContent());

Mockery::close();
}

public function testModelBindingWithNullReturn()
{
$this->expectException(ModelNotFoundException::class);
Expand Down