Skip to content

Commit

Permalink
Fix/Feature:
Browse files Browse the repository at this point in the history
- allow ImplicitRouteBinding to match compound name ('foo_bar') route parameters to method/function naming ('fooBar')
  • Loading branch information
ralphschindler committed Mar 12, 2017
1 parent b5e8c73 commit 6f7d60c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/Illuminate/Routing/ImplicitRouteBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ public static function resolveForRoute($container, $route)
foreach ($route->signatureParameters(Model::class) as $parameter) {
$class = $parameter->getClass();

if (array_key_exists($parameter->name, $parameters) &&
! $route->parameter($parameter->name) instanceof Model) {
$method = $parameter->isDefaultValueAvailable() ? 'first' : 'firstOrFail';

if (! $route->parameter($parameter->name) instanceof Model) {
$model = $container->make($class->name);

$route->setParameter(
$parameter->name, $model->where(
$model->getRouteKeyName(), $parameters[$parameter->name]
)->{$method}()
);
$parameterName = array_key_exists($parameter->name, $parameters) ? $parameter->name : null;

// check if parameter name used was camelized in routed callback method
if (!$parameterName) {
$snakeParamName = snake_case($parameter->name);
$parameterName = array_key_exists($snakeParamName, $parameters) ? $snakeParamName : null;
}

if ($parameterName) {
$value = $model->where($model->getRouteKeyName(), $parameters[$parameterName])->firstOrFail();
$route->setParameter($parameterName, $value);
}
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions tests/Routing/RoutingRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,13 @@ public function testModelBindingWithBindingClosure()
$this->assertEquals('tayloralt', $router->dispatch(Request::create('foo/TAYLOR', 'GET'))->getContent());
}

public function testModelBindingWithCompoundParameterName()
{
$router = $this->getRouter();
$router->resource('foo-bar', 'Illuminate\Tests\Routing\RouteTestResourceControllerWithModelParameter', ['middleware' => SubstituteBindings::class]);
$this->assertEquals('12345', $router->dispatch(Request::create('foo-bar/12345', 'GET'))->getContent());
}

public function testModelBindingThroughIOC()
{
$container = new Container;
Expand Down Expand Up @@ -1275,6 +1282,14 @@ public function returnParameter($bar = '')
}
}

class RouteTestResourceControllerWithModelParameter extends Controller
{
public function show(RoutingTestUserModel $fooBar)
{
return $fooBar->value;
}
}

class RouteTestClosureMiddlewareController extends Controller
{
public function __construct()
Expand Down

0 comments on commit 6f7d60c

Please sign in to comment.