cals/rsp
Requires
- php: >=7.0.0
- laravel/framework: ~5.5
This package is not auto-updated.
Last update: 2024-09-19 06:33:58 UTC
README
关于RSP
RSP是一个用于简化Laravel应用程序开发的Web应用程序库。此外,它还提供了一些高效的命令,让您在编码时更加自由。
RSP代表Repository Service and Presenter。
如何使用
安装
您可以通过运行composer require cals/rsp
来从Packagist安装RSP。
或者,您可以将"cals/rsp": "~2.0"
添加到您的composer.json
中,然后在终端中运行composer update
来安装它。
配置
安装RSP后,您应该将Cals\RSP\RSPServiceProvider::class
放入您的config/app.php
providers数组中,以便使其工作。
如果您使用Laravel 5.5及以上版本,则不需要这样做,因为Laravel会自动加载提供者。
然后您应该运行php artisan vendor:publish --tag=rsp
来发布rsp.php
。
使用
仓库
您可以使用make:repository
来创建一个仓库及其接口,或者如果您愿意,也可以手动创建它们。记住,如果您手动创建,仓库应扩展Cals\RSP\Repositories\Implementations\Repository
。
仓库如下所示
<?php namespace App\Repositories\Implementations; use App\Models\Example; use App\Repositories\Interfaces\ExampleRepositoryInterface; use Cals\RSP\Repositories\Implementations\Repository; class ExampleRepository extends Repository implements ExampleRepositoryInterface { /** * Get the full name of model. * * @return mixed */ function model() { return Example::class; } // Put your code here... }
接口如下所示
<?php namespace App\Repositories\Interfaces; interface ExampleRepositoryInterface { // Put your code here... }
如你所见,RSP使用Eloquent ORM来操作数据库,因此您应该创建模型来映射表。我们建议您将模型仅放在app/Models/
中,而不是app/
,这将使您的项目更加简洁。
我们扩展的仓库提供了九个方法
store(array $inputs)
您可以使用它来存储数据。返回值的类型是Illuminate\Database\Eloquent\Model
的子类。all()
您可以使用它来获取所有记录。请注意,返回值的类型是Illuminate\Database\Eloquent\Collection
,而不是array
。paginate(array $credentials = null, $page, $perPage = 15)
您可以使用它来分页记录。请注意,返回值的类型是Illuminate\Pagination\LengthAwarePaginator
,而不是array
。get(array $credentials = null, array $columns = ['*'])
您可以使用它来获取记录。请注意,返回值的类型是Illuminate\Database\Eloquent\Collection
,而不是array
。getRecordsSortBy(array $credentials = null, array $columns = ['*'], $field = 'id', $asc = true)
您可以使用它来根据您提供的字段获取记录。请注意,当$asc
为true
时,返回值是升序排序的,否则是降序排序的。另外,返回值的类型是Illuminate\Database\Eloquent\Collection
,而不是array
。find(array $credentials = null)
您可以使用它来找到满足条件的记录。返回值的类型是Illuminate\Database\Eloquent\Model
的子类。update(array $credentials, array $inputs)
您可以使用它来更新满足条件的记录。返回值的类型是boolean
。destroy(array $credentials)
您可以销毁满足条件的记录。返回值的类型是boolean
。builder(array $credentials = null)
您可以使用它来创建自己的方法。返回值的类型是Illuminate\Database\Eloquent\Builder
。
当您使用
get(array $columns = ['*'],array $crendentials = null)
且返回值只有一条记录时,它仍然是Illuminate\Database\Eloquent\Collection
的实例。因此,如果您只想找到一条记录且希望其类型为Illuminate\Database\Eloquent\Model
,您可以使用find(array $credentials = null)
或使用我们提供的builder()
方法自行完成。
在使用 RSP 的同时,我们在控制器中不直接使用仓库。仓库应始终提供方法供服务使用。
服务
您可以使用 make:service
创建服务和其接口,或者如果您愿意,可以手动创建它们。记住,如果手动创建,服务应继承 Cals\RSP\Services\Implementations\Service
。
服务如下所示
<?php namespace App\Services\Implementations; use App\Repositories\Interfaces\ExampleRepositoryInterface; use App\Services\Interfaces\ExampleServiceInterface; use Cals\RSP\Services\Implementations\Service; class ExampleService extends Service implements ExampleServiceInterface { /** * ExampleService constructor. * * @param ExampleRepositoryInterface $repository */ public function __construct(ExampleRepositoryInterface $repository) { $this->repository = $repository; } // Put your code here... }
接口如下所示
<?php namespace App\Services\Interfaces; interface ExampleServiceInterface { // Put your code here... }
我们扩展的服务提供了八个方法
store(array $inputs)
all()
paginate(array $credentials = null, $page, $perPage = 15)
get(array $credentials = null, array $columns = ['*'])
getRecordsSortBy(array $credentials = null, array $columns = ['*'], $field = 'id', $asc = true)
find(array $credentials = null)
update(array $credentials, array $inputs)
destroy(array $credentials)
这八个方法简单地调用仓库提供的方法。因此,您可以覆盖它以满足您的需求。返回值的类型与上面列出的 仓库 中的类型相同。
展示者
您可以使用 make:presenter
创建展示者和其接口,或者如果您愿意,可以手动创建它们。记住,如果手动创建,展示者应继承 Cals\RSP\Presenters\Implementations\Presenter
。
展示者如下所示
<?php namespace App\Presenters\Implementations; use App\Presenters\Interfaces\ExamplePresenterInterface; use Cals\RSP\Presenters\Implementations\Presenter; class ExamplePresenter extends Presenter implements ExamplePresenterInterface { // Put your code here... }
接口如下所示
<?php namespace App\Presenters\Interfaces; interface ExamplePresenterInterface { // Put your code here... }
我们扩展的展示者提供了两个方法
limitLength($field, $length = 40)
differentiateForHumans(Carbon $carbon)
第一个方法用于限制长度,第二个方法用于显示从您提供的时间到现在的时间的不同形式。
绑定
创建您的类后,需要将其放入 rsp.php
<?php /** * Created by PhpStorm. * User: Cals * Date: 2017/3/8 * Time: 14:03 */ return [ /* |-------------------------------------------------------------------------- | Repositories |-------------------------------------------------------------------------- | | This array is the list of your repository. The key should be the full | name of interface and the value should be the full name of | implementation. */ 'repositories' => [], /* |-------------------------------------------------------------------------- | Services |-------------------------------------------------------------------------- | | This array is the list of your service. The key should be the full name | of interface and the value should be the full name of implementation. */ 'services' => [], /* |-------------------------------------------------------------------------- | Presenters |-------------------------------------------------------------------------- | | This array is the list of your presenter. The key should be the full name | of interface and the value should be the full name of implementation. */ 'presenters' => [] ];
例如
'repositories' => [ 'App\Repositories\Interfaces\ExampleRepositoryInterface' => 'App\Repository\Implementations\ExampleRepository' ],
然后 Laravel 可以将接口绑定到实现。
命令
我们提供了一些命令,用于创建具有一些基本代码的文件,您可以将自己的代码放入其中以满足您的需求。
以下命令列表
make:repository
您可以使用它来创建仓库类及其接口。make:service
您可以使用它来创建服务类及其接口。make:presenter
您可以使用它来创建展示者类及其接口。rsp:generate
您可以使用它根据注册生成仓库、服务和展示者。
贡献
感谢您考虑为 RSP 架构库做出贡献!
作者
Cals Ranna
许可证
RSP 架构是一个开源库,许可协议为 MIT 许可协议。