shiftonelabs/laravel-singular-resource-routes
Composer 安装命令:
composer require shiftonelabs/laravel-singular-resource-routes
包简介
Adds support for singular resource routes to the Laravel router.
README 文档
README
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:
- The
show,edit,update, anddestroyroutes do not need a route parameter. - Since this is a singular resource, there is no
indexaction.
Versions
This package has been tested on Laravel 5.0 through Laravel 8.x, though it may continue to work on later versions as they are released. This section will be updated to reflect the versions on which the package has actually been tested.
Install
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, ];
Usage
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:
- The boolean
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 string containing the name of the singular resource. This can be used when there is only one resource that is singular in a nested resource definition. The string must contain the name of the singular resource.
- An array of strings containing the names of the singular resources. This format must be used when there are multiple singular resources in a nested resource definition, but it can be used in any scenario.
Code Example
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']]);
Contributing
Contributions are welcome. Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email patrick@shiftonelabs.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
shiftonelabs/laravel-singular-resource-routes 适用场景与选型建议
shiftonelabs/laravel-singular-resource-routes 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.79k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2018 年 01 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routes」 「laravel」 「resource」 「singular」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 shiftonelabs/laravel-singular-resource-routes 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shiftonelabs/laravel-singular-resource-routes 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 shiftonelabs/laravel-singular-resource-routes 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Block routes by IP
Generates URLs for public Puli resources.
Add the resource system to symfony application
Provides caching methods which can be easily used for caching routes.
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
统计信息
- 总下载量: 11.79k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 27
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-01-15