Allows models to define their own Url Generator implementation #2809
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This should allow models to have more control over their url generator without impeding on the default functionality (using config media-library.url_generator). This is super useful when you'd like one specific model to have media secured with something like a signed url.
By allowing a model that
HasMedia
to define an optional non-static method:getUrlGeneratorClass
which takes no parameters and returns astring
, we can drastically modify how the package generates urls JUST to that model. I've written a simple example below.Here's the example model:
And the example Url Generator:
And the example route:
And the example controller:
This would allow so many more custom implementations as well - and should be totally non-conflicting with existing behavior.
Credit where it is due, this blog post sparked this PR for me: https://felix-schmid.de/blog/2-securing-media-from-laravel-medialibrary. I was inspired to find a solution to keep my other models' functionality while also allowing specific models to "opt-out" of default behavior and "opt-into" custom logic.
As another note to this, the issue solved my primary issue - my Media Library Pro collection components needed to have private media still function without modifying any views or other components.
Also - I know that we could just create a Url generator then change the logic by using something like:
But I cannot see how this would be easier than the optional method. Will keep things clean on the implementation side as well.
Just in case someone comes across this, and it was not merged - here's a Gist documenting a way this can be accomplished: https://gist.github.com/belzaaron/bff713e7a73a489784745b7efa2b085e without this pull request.