shiftonelabs/laravel-singular-resource-routes

为 Laravel 路由器添加对单一资源路由的支持。

1.0.1 2023-04-09 05:24 UTC

This package is auto-updated.

Last update: 2024-09-09 08:18:43 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

这个 Laravel 扩展包为 Laravel 路由器添加了对单一资源路由的支持。

有时你需要处理不需要通过 ID 引用的资源。例如,你可能有一个属于当前认证用户的 profile 资源。目前,如果你想要 show 用户资料,路由将是 /profile/{profile}。然而,由于你不需要 ID 来查找当前用户的资料,如果能有一个简单的 show 路由是 /profile 那就更好了。对于 editupdatedestroy 路由也是如此。

单一资源路由生成以下路由

这些路由与当前的资源路由在两个方面不同

  1. showeditupdatedestroy 路由不需要路由参数。
  2. 由于这是一个单一资源,因此没有 index 动作。

版本

此包已在 Laravel 5.0 至 Laravel 8.x 上进行了测试,尽管它可能在发布的新版本中继续工作。本节将更新以反映已测试的版本。

安装

通过 Composer

composer require shiftonelabs/laravel-singular-resource-routes

服务提供者

此包支持 Laravel 包自动发现。因此,如果你使用 Laravel 5.5+,则无需添加服务提供者。如果你使用 Laravel 5.0 - 5.4,请将服务提供者添加到你的 config/app.php 文件中

'providers' => [
    ...
    ShiftOneLabs\SingularResourceRoutes\SingularResourceRoutesServiceProvider::class,
];

用法

为了创建新的单一资源路由,在资源路由定义中添加了一个新的 singular 选项。此选项可以设置为三个不同的值

  1. 布尔值 true。这主要用于非嵌套资源定义。如果在嵌套资源定义中使用,则只有链中的最后一个资源被视为单一资源。
  2. 包含单一资源名称的字符串。这可以在嵌套资源定义中只有一个单一资源是单一资源时使用。该字符串必须包含单一资源的名称。
  3. 包含单一资源名称的字符串数组。此格式必须在嵌套资源定义中有多个单一资源时使用,也可以在任何情况下使用。

代码示例

单一资源(/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']]);

嵌套在复数资源下的单一资源(/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']]);

嵌套在单一资源下的单一资源(/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']]);

嵌套在单一资源下的复数资源(/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']]);

嵌套在复数资源下的单一资源嵌套在单一资源下(/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 获取详细信息。

安全性

如果你发现任何安全问题,请通过电子邮件 patrick@shiftonelabs.com 而不是使用问题跟踪器。

鸣谢

许可证

MIT 许可证(MIT)。请参阅 许可证文件 获取更多信息。