silvertipsoftware / rest-router
使用类和模型进行智能URL构建
Requires (Dev)
- orchestra/testbench: ^4.0
README
关于
rest-router
包允许您通过传递 Eloquent 模型或对应的类名来构建 URL。
特性
- 使用类和模型进行智能 URL 构建。
安装
在您的 composer.json
中要求 silvertipsoftware/rest-router
包并更新您的依赖项
$ composer require silvertipsoftware/rest-router
该包可以通过直接在 SilvertipSoftware\RestRouter\RestRouter
上调用 url()
或 path()
来使用,但为了方便,还提供了一个 Laravel 内置 URL
门面的混合。要使用,在您的 RouteServiceProvider::boot
方法中调用
URL::mixin(new \SilvertipSoftware\RestRouter\UrlMixins);
url()
和 path()
方法现在将可在 URL
上使用。
用法
RestRouter
主要用于在 Laravel 路由器中定义的带有 Route::resource()
方法的路由上工作,尽管也可以使用手动编写的路由。在幕后,RestRouter
使用路由名称来创建 URL;实际的 URL 文本是不相关的。
简单资源路由
假设已经定义了以下资源
Route::resource('posts', 'PostsController');
然后在您的代码中可以这样做
$post = Post::find(123); $url = URL::url($post); // $url = https://mysite.com/posts/123 $path = URL::path(Post::class); // $path = /posts $path = URL::path(new Post); // $path = /posts $url = URL::url($post, 'edit'); // $url = https://mysite.com/posts/123/edit $path = URL::path(Post::class, 'create'); // $path = /posts/create
回退
并非每个 REST 动作都需要定义在资源上。RestRouter
会检查生成相同 URL 的其他路由名称,因为它不关心使用的 HTTP 方法。例如
Route::resource('logs', 'LogsController')->only(['store', 'destroy']); // the 'logs.index' route doesn't need to be defined: $path = URL::path(Log::class); // $path = /logs // the 'logs.show' route doesn't need to be defined: $path = URL::path($logEntry); // $path = /logs/123
带前缀的资源路由
RestRouter
需要提供任何定义的路由名称前缀(不是 URI 前缀)。例如
Route::prefix('backoffice')->name('admin.')->group(function () { Route::resource('logs', 'LogsController'); }); $path = URL::path('admin', Log::class); // $path = /backoffice/logs $path = URL::path('backoffice', Log::class); // errors
非 REST 动作
如果您已经在资源上定义了非常规的 REST 动作,您可以传递一个 action
选项到 RestRouter
来查找该路由名称。例如
Route::get('/posts/{post}/metadata', 'PostsController@metadata')->name('posts.metadata'); $path = URL::path($post, ['action' => 'metadata']); // $path = /posts/123/metadata
嵌套资源
RestRouter
默认假设浅层资源。如果您使用嵌套资源,这可以在全局或按路由的基础上启用。
要全局启用嵌套资源支持,在您的 RouteServiceProvider
中插入
RestRouter::$shallowResources = false;
在单个路由上,将 "shallow" => false
选项传递给您的 url()
或 path()
调用,如下所示。例如
Route::resource('posts.comments', 'CommentsController'); $path = URL::path($post, $comment, ['shallow' => false]); // $path = /posts/123/comments/456
路由生成选项
如果最后一个参数是数组,它被视为一个键值数组,用于修改 URL。可用的选项有
$options = [ 'shallow' => false, // defaults to true 'action' => 'edit', // RESTful actions 'create', 'edit' etc. or any other action you've defined 'format' => 'json' // added as an extension to the uri, can be anything ]
添加到 $options
参数中的任何其他键都被视为生成的 URL 的查询参数,或者如果它们被定义,则作为路由参数。
Route::resource('posts', 'PostsController'); Route::get('/{account_id}/logs/{log}')->name('logs.show'); $path = URL::path($log, ['account_id'=>111]); // $path = /111/logs/123 $url = URL::url($post, ['action' => 'edit']); // $url = https://mysite.com/posts/456/edit $path = URL::path($post, ['format' => 'json']); // $path = /posts/456.json $path = URL::path($post, ['mode' => 'full']); // $path = /posts/456?mode=full
也适用于 blade 模板
<a href="{{ URL::url($post) }}"</a>
将渲染为
<a href="https://mysite.com/posts/123"</a>
许可证
在 MIT 许可证下发布,请参阅 LICENSE。