Package Data | |
---|---|
Maintainer Username: | patrickcarlohickman |
Maintainer Contact: | patrick@shiftonelabs.com (Patrick Carlo-Hickman) |
Package Create Date: | 2018-01-14 |
Package Last Update: | 2023-04-09 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-11 15:07:26 |
Package Statistics | |
---|---|
Total Downloads: | 9,184 |
Monthly Downloads: | 163 |
Daily Downloads: | 1 |
Total Stars: | 26 |
Total Watchers: | 3 |
Total Forks: | 0 |
Total Open Issues: | 0 |
This Laravel package adds support for singular resource routes to Laravel's router.
Sometimes you need to deal with a resource that does not need to be referenced by the id. For example, you may have a profile
resource that belongs to the currently authenticated user. Currently, if you wanted to show
the user's profile, the route would be /profile/{profile}
. However, since you don't need the id to look up the current user's profile, it would be nice to be able to simply have the show
route be /profile
. This is the same for the edit
, update
, and destroy
routes, as well.
The singular resource route generates the following routes:
| Verb | Path | Action | Route Name | | -- | -- | -- | -- | | GET | /profile/create | create | profile.create | | POST | /profile | store | profile.store | | GET | /profile | show | profile.show | | GET | /profile/edit | edit | profile.edit | | PUT/PATCH | /profile | update | profile.update | | DELETE | /profile | destroy | profile.destroy |
These routes differ from the current resource routes in two ways:
show
, edit
, update
, and destroy
routes do not need a route parameter.index
action.Via Composer
$ composer require shiftonelabs/laravel-singular-resource-routes
Service Provider
This package supports Laravel package autodiscovery. So, if using Laravel 5.5+, there is no need to add the server provider. If using Laravel 5.0 - 5.4, add the service provider to your config/app.php
file:
'providers' => [
...
ShiftOneLabs\SingularResourceRoutes\SingularResourceRoutesServiceProvider::class,
];
In order to create a new singular resource route, a new singular
option is added to the resource route definition. This option can be set to three different values:
true
. This is mainly for non-nested resource definitions. If it is used on a nested resource definition, only the last resource in the chain will be treated as singular.A singular resource (/profile
):
// Since there is only one singular resource, and it is the last one,
// all three options work in this scenario.
Route::resource('profile', 'ProfileController', ['singular' => true]);
Route::resource('profile', 'ProfileController', ['singular' => 'profile']);
Route::resource('profile', 'ProfileController', ['singular' => ['profile']]);
A singular resource nested under a plural resource (/users/{user}/profile
):
// Since there is only one singular resource, and it is the last one,
// all three options work in this scenario.
Route::resource('users.profile', 'ProfileController', ['singular' => true]);
Route::resource('users.profile', 'ProfileController', ['singular' => 'profile']);
Route::resource('users.profile', 'ProfileController', ['singular' => ['profile']]);
A singular resource nested under a singular resource (/profile/avatar
):
// Since there are multiple resources that are singular, only the array
// syntax can be used to specify all the singular resources.
Route::resource('profile.avatar', 'ProfileAvatarController', ['singular' => ['profile', 'avatar']]);
A plural resource nested under a singular resource (/profile/phones/{phone}
):
// Since there is a parent resource that is singular, the "true" option cannot be used.
// But since there is only one singular resource, the string option can be used.
Route::resource('profile.phones', 'PhoneController', ['singular' => 'profile']);
Route::resource('profile.phones', 'PhoneController', ['singular' => ['profile']]);
A singular resource nested under a plural resource nested under a singular resource (/profile/phones/{phone}/type
):
// Since there are multiple resources that are singular, only the array
// syntax can be used to specify all the singular resources.
Route::resource('profile.phones.type', 'PhoneTypeController', ['singular' => ['profile', 'type']]);
Contributions are welcome. Please see CONTRIBUTING for details.
If you discover any security related issues, please email patrick@shiftonelabs.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.