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

Override route key name if $primaryKey is overridden #630

Merged
merged 4 commits into from
Apr 20, 2023
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
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';
}
}