-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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
[5.5] Route model binding with key in route definition #18197
[5.5] Route model binding with key in route definition #18197
Conversation
I like this. Definitely needs some tests though. |
I will definitely do that as soon as possible |
Thinking I might create a dedicated RouteParameter class to tidy up the |
return is_string($value) && strlen($value) > 0; | ||
}); | ||
foreach ($parameterNames as $value => $name) { | ||
$name_without_key = preg_replace('/:\w+/', null, $name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use camel-case variable instead: $nameWithoutKey
if (isset($routeParameters[$index])) { | ||
$key = $routeParameters[$index]; | ||
} | ||
$parts = explode(':', isset($key) ? $key : $index); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's looking like this code is duplicated...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ($parameter instanceof UrlRoutable) { | ||
$parameters[$key] = $parameter->getRouteKey(); | ||
if ($parameter instanceof Model) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would imply to require illuminate/database
to illuminate/routing
, I am not sure this is worth it.
For now only \Illuminate\Database\Eloquent\Model
implements \Illuminate\Contracts\Routing\UrlRoutable
.
Why not just checking that getAttribute
method key exists ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree and I changed it, but for the record: it's also being used here: https://github.com/laravel/framework/blob/5.4/src/Illuminate/Routing/ImplicitRouteBinding.php#L5
illuminate/database
is not in the composer.json though...
I would go for it |
@lucasmichot Done, what do you think? |
👍
Damn, did not see that. I suppose you can thus check that |
Remove return type for `parameter()` method
* RouteParameter constructor. | ||
* | ||
* @param string $parameter | ||
* @param mixed $value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing @return void
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
@@ -359,15 +360,22 @@ protected function formatAction($action) | |||
* Format the array of URL parameters. | |||
* | |||
* @param mixed|array $parameters | |||
* @param Route $route |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@param \Illuminate\Routing\Route $route
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
src/Illuminate/Routing/Route.php
Outdated
@@ -426,6 +426,18 @@ public function signatureParameters($subClass = null) | |||
} | |||
|
|||
/** | |||
* Get the parameters as instances of RouteParameter. | |||
* | |||
* @return RouteParameter[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually use @return array
notation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed.
*/ | ||
protected function parts() | ||
{ | ||
return explode(':', $this->parameter); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return explode(':', $this->parameter, 2);
maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't really matter, but added anyway.
tests/Routing/RoutingRouteTest.php
Outdated
@@ -1495,6 +1516,10 @@ class RoutingTestExtendedUserModel extends RoutingTestUserModel | |||
{ | |||
} | |||
|
|||
class RoutingTestWithRouteKey extends RoutingTestUserModel | |||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we use an empty comment for empty classes, as for RoutableModelStub
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.
tests/Routing/RouteParameterTest.php
Outdated
public function testParsingName() | ||
{ | ||
$parameter = new RouteParameter('baz'); | ||
$this->assertEquals('baz', $parameter->name()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose al assertEquals
comparing a string can be replace by stricter assertSame
assertions ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you say so 😄
Changed.
…/framework into route-model-binding-key
A conflict needs to be solved @sailingdeveloper |
@lucasmichot I don't see it, can you give me a line number? |
Line 38 |
Either I'm blind, or we're not looking at the same file(s). Can you tell me what the conflict is, by screenshot or some other way? |
! $route->parameter($parameter->name) instanceof Model) { | ||
$model = $container->make($class->name); | ||
foreach ($routeParameters as $routeParameter) { | ||
if ($routeParameter->name() == $parameter->name && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not using a stricter comparison ===
?
I think I'm going to leave this to manual use of |
That's a bummer. Too bad |
Any news on this feature? |
This would have been so sweet to have in Laravel |
Would really like to see this implemented. Adds a really elegant way for slugs to be used on a user facing interface and IDs for an admin facing interface. It also would allow the admin interface to change a slug without the need to redirect to the new slug on form submit. |
See laravel/ideas#260
This allows for the following:
I realise that this is far from pretty or the ideal implementation, but I am very open to suggestions/edits from the community!