arch/repositories

中心仓库,在控制器和模型之间传递和接收数据

1.5.1 2017-03-13 13:57 UTC

This package is auto-updated.

Last update: 2024-09-23 13:52:33 UTC


README

Laravel 数据库中心仓库数据传输包。此包作为控制器、模型和数据库之间的桥梁。所有可重用代码应位于一个地方,并在需要时调用。

包安装

  1. 可以使用 composer 安装此包,只需添加依赖

    composer require arch/repositories

  2. 安装后,将 ArchServiceProvider 添加到 config/app.php 文件中的 provider 数组中,如下所示

'providers' => [
  ............
  Arch\Repositories\ServiceProvider\ArchServiceProvider::class
];

Canal、仓库和模型安装

在可以使用任何功能之前,我们必须为应用程序创建 CanalRepositories,并使用特质类来利用 Model

1) 生成仓库

Repositories 是一个普通的 PHP 类,具有所有与数据库查询相关的 Laravel 查询构建器 功能。您可以在 可用方法 部分找到所有可用的方法。使用以下命令生成 repositories

php artisan shareable:repositories <RepositoriesName> --model=<modelName>

eg : 

php artisan shareable:repositories UserRepositories --model=User

RepositoriesName - 仓库名称

modelName - 模型名称。此模型与仓库相关联。

此命令将在 App\Repositories 目录中动态创建 Repositories 文件。一个仓库应有一个与之关联的模型。为了便于使用,请创建具有描述性名称的仓库。例如:UserRepositories,这样我们知道这个仓库属于 User 模型。

2) 生成 Canal

Canal 是一个普通的 PHP 类,具有所有可重用的方法,这些方法创建用于在 控制器 中调用的目的。我们没有直接在 控制器 中调用模型,而是使用此 Canal 类来管理数据传递/检索。此类应包含调用 Repositories 方法的函数。

php artisan shareable:canal <CanalName>

eg : 

php artisan shareable:canal UserModuleCanal

CanalName - Canal 名称。

此命令将在 App\Canal 目录中动态创建 Canal 文件。此类应包含所有可重用代码。例如,我们有一个 UserModule 用于简单的/复杂的 CRUD 操作、用户权限等。然后,所有方法都可以放在 UserModuleCanal 中,而 UserModuleCanal 可以在其内部调用 Repositories 的任何方法。稍后,如果我们有两个用于 Web 应用程序和 Api 的控制器,它们具有相同的行为,我们可以为两个 控制器 调用 UserModuleCanal,因为它们都具有相同的功能。

3) 在模型中使用特质

最后,在关联 RepositoriesModel 中包含特质 Arch\Util\Instantiate,如下所示

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Arch\Util\Instantiate;

class User extends Model {
   use Instantiate;    
}

按照这 3 个步骤为另一个模块执行。完成!

使用示例

  1. 首先,在 Controller 中包含 Canal 类,例如:UserController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Canal\UserModuleCanal as UserCanal; // Canal class

class UserController extends Controller
{
    private $userModule = null;
    
    public function __construct () {
        // instantiate and assign to class member
        $this->userModule = new UserCanal;
    }
    // retrieving all users
    public function index () {
    	
    	dd( $this->userModule->getAllUser() );

    }
}
  1. Canal 类中创建可重用代码,例如:UserModuleCanal
<?php

namespace App\Canal;

use App\Repositories\UserRepositories as UserRepo; // include Repositories
use DB; // for creating custom query


class UserCanal {	
	
	// write method to call Repositories class
	public function getAllUser () {

		$user = new UserRepo; // Instantiate UserRepo object
		return $user->all();  // call UserRepo method and pass data back to Controller
        
	}

}
  1. 仓库的简单示例,例如:UserRepositories
<?php

namespace App\Repositories;

use Arch\Repositories\Shareables\BaseShareables as BaseAbstract;
use App\User; // Model tied with this repositories
use DB;       // custom database querying

class UserRepositories extends BaseAbstract {	
	
	public function __construct() {
		// Assign to parent Model for data fetching
		$this->model = User::getInstance();
	}
	
    // At here we didn't create all() method, basically we didnt
    // create any method inside this repositories 
    // as all the laravel Query method already
    // available inside this repositories
    // by include this BaseShareables class
    // unless you need complex query that BaseShareables didnt provided for you
    
    // You can do any custom query from database in here
    // by creating custom method, later on, we can call
    // this method inside `Canal` class
}
  1. 将路由映射到该控制器,然后就可以使用了。

内置实用工具/库

  • 加密(使用 laravel 加密和 Hashids by @ivanakimov)
  • MultiCurl
  • 将来还会添加更多实用工具和库...

1) 加密

在控制器中直接在命名空间关键字之后要求该实用工具

use Arch\Libs\Fence

使用示例

a) 加密数据

返回加密数据

$encrypted = Fence::encrypt( $var );

b) 解密数据

返回解密数据

$encrypted = Fence::decrypt( $var );

c) 哈希

返回哈希数据

$hashed = Fence::hash( $var );

d) 比较

如果成功,则返回解密数据,否则返回 false

$hashed = 'KJNksdsjdhjunKLJ....';
$encrypted = 'SDxkzjxncjn...';
$encrypted = Fence::compare( $encrypted,$hashed );

e) 编码(Hashids)

返回加密数据

$encode = Fence::encode( $var ); // produce 15 length alpha numeric characters

f) 解码(Hashids)

返回解密数据

$decode = Fence::decode( $var );

g) 匹配(Hashids)

如果成功,则返回解密数据,否则返回 false

$encode = 'KJNksdsjdhjunKLJ....';
$hashed = 'SDxkzjxncjn...';
$hashed = Fence::match( $encode, $hashed );

2) MultiCurl

这个工具与MultiCurl相同,因为这个包在安装时使用composer自动安装。在控制器内部直接在命名空间关键字后使用此工具。

use Arch\Libs\MultiCurl

使用示例:示例可以直接在MultiCurl中找到。唯一的区别是您需要使用use Arch\Libs\MultiCurl而不是单独使用use Arch\MultiCurl\MultiCurl;。但是两者都是相同的。选择权在你。

支持

  • 好的,只需提交一个issue

贡献

欢迎fork并创建PR

作者