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
[9.x] Add Route::singleton()
method
#44872
base: 9.x
Are you sure you want to change the base?
Conversation
* @param array $options | ||
* @return void | ||
*/ | ||
public function __construct(ResourceRegistrar $registrar, $name, $controller, array $options) |
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 use PHP 8 constructor property here
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 class matches the PendingResourceRegistration
class that was created prior to PHP 8.
This looks super useful, @jessarcher! |
In your examples you have routes to destroy a singleton. Shouldn't you also be able to create and store one if it doesn't exist? |
The intention would be that you'd always make a |
That makes sense, @jessarcher. |
What about Anyway, nice addition! |
Nice. I've definitely created these types of resources by hand before. My only change would be the name. If this is always a resource, which it seems to be, I'd suggest |
This PR introduces a helper to register routes for "singleton resources" - resources that are either "one of one" or "zero or one".
Example:
/profile
/profile/edit
/profile
/profile
Many of the options that apply to resource controllers can also be used here, such as
only
andexcept
:When the resource can be zero or one, a PUT request can be used for both creating and updating the resource because we always know the resource's location. The
update
name that is traditionally attached to thePUT
doesn't make this entirely clear, so we could potentially name it something likeupsert
instead. Personally, I prefer keeping the existing name rather than introducing a new action name.Singleton resources can also be nested within a standard resource for cases where a resource is singular in the scope of the parent resource:
/videos/{video}/thumbnail
/videos/{video}/thumbnail/edit
/videos/{video}/thumbnail
/videos/{video}/thumbnail
Route model binding would work for the
Video
model in the above example, but there is no model binding for singleton resources because the URL does not provide enough information to resolve it. In many cases, there may not even be a dedicated model for the singleton resource. In the above example, the thumbnail could be a separate model, but it could equally just be an attribute on theVideo
model that warrants its own routes.For parity with the
resource
methods, there is also anapiSingleton
method that excludes theedit
action, and there aresingletons
andapiSingletons
methods for bulk registration.