ssmalik99 / repostarry
这是一个辅助包,旨在让 Laravel 用户轻松创建仓库系统
V1.0.2
2022-07-09 12:24 UTC
Requires (Dev)
- orchestra/testbench: ^7.6
README
Starry 是一个用于在 Laravel (PHP 框架) 中创建标准仓库系统的小型包。
安装
通过 composer 安装
composer require ssmalik99/repostarry
使用
启动 Starry
php artisan starry:launch
此命令将为 starry 启动基本设置并发布一个配置文件 starry.php
以管理 starry 包的配置。
更改配置
如果您想使用其他文件夹结构,可以通过更改 .env
变量来实现
如果我们遵循默认设置,则此配置文件将出现
<?php
return [
/*
* ***************************************
* Which data model our project is using *
* ***************************************
*/
"starry_data_model" => env('STARRY_DATA_MODEL', "Eloquent"),
/*
* ********************************************
* Path where we want to store out interfaces *
* ********************************************
*/
'starry_interfaces_path' => env("STARRY_INTERFACES_PATH", "StarryInterfaces"),
/*
* ********************************************
* Where We want to store our main repository *
* ********************************************
*/
"starry_repository_path" => env("STARRY_REPOSITORY_PATH", "Eloquent"),
'bindings' => [
\App\Repository\StarryInterfaces\EloquentRepositoryInterface::class => \App\Repository\Eloquent\BaseRepository::class,
],
];
最初,Starry 将根据上述配置遵循此结构。
---App
------Repository
----------------Eloquent(Default Setup)
---------------------------------------BaseRepository.php
----------------StarryInterfaces(Default Setup)
----------------------------------------------EloquentRepositoryInterface.php(Default Setup)
除了默认仓库之外的所有仓库都将扩展 BaseRepository
以减少代码。
创建您的 Starry
php artisan make:starry model_name
此命令需要一个 model_name
,您希望为它创建仓库和接口
例如
php artisan make:starry User
这将创建 UserRepositoryInterface
、UserRepository
并将它们绑定到我们的 Laravel 应用中。
绑定
对于 starry,绑定是如何实际工作的?
在我们的配置文件夹中的 starry.php
文件内,我们有所有的绑定。
'bindings' => [
\App\Repository\StarryInterfaces\EloquentRepositoryInterface::class => \App\Repository\Eloquent\BaseRepository::class,
],
因此,如果您已手动创建您的仓库,并想通过 starry 来绑定它,只需将您的 interface
作为 key
,将 repository_class
作为 value
添加到 bindings
数组中的元素即可。