Skip to content

Commit

Permalink
Merge pull request #630 from Jared0430/patch-1
Browse files Browse the repository at this point in the history
Override route key name if $primaryKey is overridden
  • Loading branch information
bakerkretzmar authored Apr 20, 2023
2 parents ded2249 + d42d904 commit 396516d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use JsonSerializable;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;

class Ziggy implements JsonSerializable
{
Expand Down Expand Up @@ -199,7 +200,11 @@ private function resolveBindings(array $routes): array
? Reflector::getParameterClassName($parameter)
: $parameter->getType()->getName();
$override = (new ReflectionClass($model))->isInstantiable()
&& (new ReflectionMethod($model, 'getRouteKeyName'))->class !== Model::class;
&& (
(new ReflectionMethod($model, 'getRouteKeyName'))->class !== Model::class
|| (new ReflectionMethod($model, 'getKeyName'))->class !== Model::class
|| (new ReflectionProperty($model, 'primaryKey'))->class !== Model::class
);

// Avoid booting this model if it doesn't override the default route key name
$bindings[$parameter->getName()] = $override ? app($model)->getRouteKeyName() : 'id';
Expand Down
67 changes: 66 additions & 1 deletion tests/Unit/RouteModelBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ protected function setUp(): void
$router->post('users', function (User $user) {
return '';
})->name('users.store');
$router->get('comments/{comment}', function (Comment $comment) {
return '';
})->name('comments');
$router->get('replies/{reply}', function (Reply $reply) {
return '';
})->name('replies');

if ($this->laravelVersion(7)) {
$router->get('blog/{category}/{post:slug}', function (PostCategory $category, Post $post) {
Expand Down Expand Up @@ -187,6 +193,20 @@ public function can_merge_implicit_and_scoped_bindings()
'uri' => 'users',
'methods' => ['POST'],
],
'comments' => [
'uri' => 'comments/{comment}',
'methods' => ['GET', 'HEAD'],
'bindings' => [
'comment' => 'uuid',
],
],
'replies' => [
'uri' => 'replies/{reply}',
'methods' => ['GET', 'HEAD'],
'bindings' => [
'reply' => 'uuid',
],
],
'posts' => [
'uri' => 'blog/{category}/{post}',
'methods' => ['GET', 'HEAD'],
Expand Down Expand Up @@ -214,7 +234,7 @@ public function can_include_bindings_in_json()
$this->markTestSkipped('Requires Laravel >=7');
}

$json = '{"url":"http:\/\/ziggy.dev","port":null,"defaults":{},"routes":{"users":{"uri":"users\/{user}","methods":["GET","HEAD"],"bindings":{"user":"uuid"}},"admins":{"uri":"admins\/{admin}","methods":["GET","HEAD"],"bindings":{"admin":"uuid"}},"tags":{"uri":"tags\/{tag}","methods":["GET","HEAD"],"bindings":{"tag":"id"}},"tokens":{"uri":"tokens\/{token}","methods":["GET","HEAD"]},"users.numbers":{"uri":"users\/{user}\/{number}","methods":["GET","HEAD"],"bindings":{"user":"uuid"}},"users.store":{"uri":"users","methods":["POST"]},"posts":{"uri":"blog\/{category}\/{post}","methods":["GET","HEAD"],"bindings":{"category":"id","post":"slug"}},"posts.tags":{"uri":"blog\/{category}\/{post}\/{tag}","methods":["GET","HEAD"],"bindings":{"category":"id","post":"slug","tag":"slug"}}}}';
$json = '{"url":"http:\/\/ziggy.dev","port":null,"defaults":{},"routes":{"users":{"uri":"users\/{user}","methods":["GET","HEAD"],"bindings":{"user":"uuid"}},"admins":{"uri":"admins\/{admin}","methods":["GET","HEAD"],"bindings":{"admin":"uuid"}},"tags":{"uri":"tags\/{tag}","methods":["GET","HEAD"],"bindings":{"tag":"id"}},"tokens":{"uri":"tokens\/{token}","methods":["GET","HEAD"]},"users.numbers":{"uri":"users\/{user}\/{number}","methods":["GET","HEAD"],"bindings":{"user":"uuid"}},"users.store":{"uri":"users","methods":["POST"]},"comments":{"uri":"comments\/{comment}","methods":["GET","HEAD"],"bindings":{"comment":"uuid"}},"replies":{"uri":"replies\/{reply}","methods":["GET","HEAD"],"bindings":{"reply":"uuid"}},"posts":{"uri":"blog\/{category}\/{post}","methods":["GET","HEAD"],"bindings":{"category":"id","post":"slug"}},"posts.tags":{"uri":"blog\/{category}\/{post}\/{tag}","methods":["GET","HEAD"],"bindings":{"category":"id","post":"slug","tag":"slug"}}}}';

$this->assertSame($json, (new Ziggy)->toJson());
}
Expand Down Expand Up @@ -244,6 +264,38 @@ public function can_handle_abstract_classes_in_route_model_bindings()
],
], (new Ziggy)->toArray()['routes']['models']);
}

/** @test */
public function can_use_custom_primary_key()
{
$expected = [
'comments' => [
'uri' => 'comments/{comment}',
'methods' => ['GET', 'HEAD'],
'bindings' => [
'comment' => 'uuid',
],
],
];

$this->assertSame($expected, (new Ziggy)->filter('comments')->toArray()['routes']);
}

/** @test */
public function can_use_get_key_name()
{
$expected = [
'replies' => [
'uri' => 'replies/{reply}',
'methods' => ['GET', 'HEAD'],
'bindings' => [
'reply' => 'uuid',
],
],
];

$this->assertSame($expected, (new Ziggy)->filter('replies')->toArray()['routes']);
}
}

class User extends Model
Expand Down Expand Up @@ -287,3 +339,16 @@ class Admin extends User
{
//
}

class Comment extends Model
{
protected $primaryKey = 'uuid';
}

class Reply extends Model
{
public function getKeyName()
{
return 'uuid';
}
}

0 comments on commit 396516d

Please sign in to comment.