定制 rolice/laravel-db-switch 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rolice/laravel-db-switch

Composer 安装命令:

composer require rolice/laravel-db-switch

包简介

Simple and light DB switcher for Laravel.

README 文档

README

Composer package for Laravel that enables easy replacement of database instance. The package is highly suitable for similar databases that run different copies of a same program.

This package is being developed and tested under Laravel 5.3, Laravel 5.4 and Lumen 5.4. However it should be compatible with the older releases of Laravel, expecting at least as versions 5.0.

Prerequisites

Composer

You will need composer to setup your project. You can skip this point most-likely, since it is a must for your Laravel or Lumen project, but if for some reason you are new to it you will find the official composer website very useful. You will find detailed documentation, use-cases and scenarios plus full instruction set about the downloading and installing composer, including all the download files needed.

Laravel/Lumen Project

We assume you have prepared and installed already your Laravel or Lumen project and you have navigated to its folder with your console/terminal application.

Installation

The package is installed in the traditional way through composer. You can do this by executing the following command in the folder of your Laravel project:

composer require 'rolice/laravel-db-switch' # with globally installed composer

or if you have no global composer installation exists with your project, but simply composer.phar file:

php /path/to/composer.phar require 'rolice/laravel-db-switch' # with local composer.phar file

The above should add the package with your project directly.

Alternatively, you can manually add rolice/laravel-db-switch in your composer.json file in the require section and then you will be able to install it with:

composer install

or again if no global composer installation exists:

php /path/to/composer.phar install

Note: That you will have to replace /path/to/composer.phar in the examples above with the actual path where you have downloaded a copy of the official composer.phar. More information could be found on the previous section with the Prerequisites.

After you are ready with the package installation we have to enable the service provider inside the application config.

For Laravel: Just open the {your/project/folder}/config/app.php file of your application (by default should be located there).

Add the service provider inside the providers section (array):

Rolice\LaravelDbSwitch\DbSwitchServiceProvider::class,

Preferably under the comment like:

/*
 * Package Service Providers...
 */
 Rolice\LaravelDbSwitch\DbSwitchServiceProvider::class,

For Lumen: You should register the service provider inside bootstrap/app.php like:

/*
|--------------------------------------------------------------------------
| Register Service Providers
|--------------------------------------------------------------------------
|
| Here we will register all of the application's servipoce providers which
| are used to bind services into the container. Service providers are
| totally optional, so you are not required to uncomment this line.
|
*/
// ...
$app->register(Rolice\LaravelDbSwitch\DbSwitchServiceProvider::class);
// ...

For Laravel: Now you can register the facade in the section below named aliases (array), in the same file config/app.php by adding the following like there:

'DbSwitch' => Rolice\LaravelDbSwitch\Facades\DbSwitch::class,

For Lumen: You can enable facades and pass it along with:

$app->withFacades(true, [
    Rolice\LaravelDbSwitch\Facades\DbSwitch::class => 'DBSwitch'
]);

...or you can directly enable it the same way, but with raw code, an example:

/*
|--------------------------------------------------------------------------
| Register Facades
|--------------------------------------------------------------------------
|
| A config section for registering facades through class aliases.
|
*/

class_alias(\Rolice\LaravelDbSwitch\Facades\DbSwitch::class, 'DbSwitch');

Now the package should be available and running with your project.

Usage

You can use the package service either through the facade DbSwitch or through the singleton instance inside the IoC service container of Laravel:

// Usage through the facade - DbSwitch
DbSwitch::to('my-cool-db'); // The defaut connection
DbSwitch::connectionTo('my-cool-conenction', 'my-cool-db'); // A specific connection database

// Usage through the Laravel Service Container (IoC)
app('db.switch')->to('my-cool-db'); // The defaut connection
app('db.switch')->connectionTo('my-cool-conenction', 'my-cool-db'); // A specific connection database

That is the whole scope of this package.

Enjoy switching your databases! :P

统计信息

  • 总下载量: 1.86k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-10-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固