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

Make explicit binding for products conditional in hub context #1312

Merged

Conversation

repl6669
Copy link
Contributor

While developing lunar-api, specifically implementing the possibility of using hashids, I came across an issue with explicit model binding for products.

The underlying packages encodes and decodes route keys on runtime. The login can be seen here. When AdminHubServiceProvider binds the product explicitly, the route key does not get decoded.

It is also quite difficult to overwrite such binding by other packages. You would have to ensure that the order of service providers is correct in order to "rebind" the routes.

These are the problematic lines:

Route::bind('product', function ($id) {
return Product::withTrashed()->findOrFail($id);
});

I came up with a simple solution for now:

https://github.com/dystcz/lunar/blob/9eba7aa9e45f19d6d08c8a08f7f55656df213666/packages/admin/src/AdminHubServiceProvider.php#L221-L227

This applies the binding only for routes with hub prefix.

Another solutions would be to implement the binding via middleware or by overwriting the resolveRouteBinding method on the model.

@vercel
Copy link

vercel bot commented Oct 19, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lunar-docs ❌ Failed (Inspect) Dec 19, 2023 9:39pm

Copy link
Collaborator

@alecritson alecritson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @theimerj and while I can see there is an issue here I don't think this is correct, when doing some initial testing and editing a product, $route->getPrefix() returns hub/products/{product} so the condition wouldn't match.

I think we'd need a more standard approach to determine whether it's a hub request, not just by going off the configured hub path. 🤔

@ryanmitchell
Copy link
Contributor

Check if its using the hub middleware?

@glennjacobs glennjacobs added the enhancement Improvement to existing functionality label Dec 18, 2023
@glennjacobs glennjacobs added this to the v0.7 milestone Dec 18, 2023
@glennjacobs
Copy link
Contributor

@theimerj could you retarget the 0.7 branch, please?

@repl6669 repl6669 changed the base branch from 0.6 to 0.7 December 19, 2023 15:16
@repl6669
Copy link
Contributor Author

repl6669 commented Dec 19, 2023

Hey @glennjacobs, I retargeted to 0.7 and refactored it to check against Hub Authenticate middleware based on @ryanmitchell's idea. I also added a test which proves that soft deleted products can be accessed in the hub.

If you know about some better way how to determine that the route belongs to hub scope, let me know, i will update the code accordingly.

@alecritson alecritson merged commit 380012a into lunarphp:0.7 Dec 22, 2023
12 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvement to existing functionality
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

9 participants