iceylan / restorm
一个Laravel插件,用于和谐地集成运行在客户端的Restorm。
v2.0.0
2024-06-13 17:15 UTC
Requires
- php: ^8.0
README
此Composer包允许我们正确处理由客户端的Restorm JavaScript库发送的Laravel中的请求。
安装
要安装此包,请在终端中运行以下命令,在Laravel项目的根路径下:
composer require iceylan/restorm
用法
让我们保持简单,我们可以在laravel routes/api.php
文件中使用它,如下所示:
use App\Models\Post; use Iceylan\Restorm\Restorm; Route::get( 'v1.0/posts', function( Restorm $restorm ) { return $restorm ->apply( Post::class ) ->get(); });
服务器可以轻松响应请求,如下所示:
GET /api/v1.0/posts?with=author&filter[author_id]=eq:12&sort[created_at]=desc&limit=10&page=1&paginate=length-aware
如果端点是资源集合,那么我们应该像Laravel一样获取模型:
use App\Models\Post; use Iceylan\Restorm\Restorm; Route::get( 'v1.0/posts/{post}', function( Restorm $restorm, Post $post ) { return $restorm ->apply( $post ) ->get(); });
我们可以将以下类型作为参数传递给apply
方法:
- 完全限定的类名,如
App\Models\Post
- 查询构建器,如
Post::where( 'id', 1 )
- 关系,如
Post::with( 'user' )
- 模型实例,如
Post::find( 1 )
- 模型类,如
Post::class
,这将返回App\Models\Post
Restorm将解析请求,并根据请求中携带的指令修改我们传递给它的模型。
它将返回QueryBuilder、Model、Relation或我们最初传递的任何内容。它将带有关联的修改返回。我们可以继续像在Laravel中一样使用这个源。