Skip to content
This repository has been archived by the owner on May 10, 2023. It is now read-only.
/ dbchanger Public archive

Change your database with request header. Laravel 5.

License

Notifications You must be signed in to change notification settings

mintellity/dbchanger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

dbchanger

Change your database with request header. Laravel 5.

Installation

Require package in your composer.json:

"mintellity/dbchanger" : "dev-master"

Publish config file:

php artisan vendor:publish --provider="Mintellity\DbChanger\DbChangerServiceProvider"

Make sure the right user is set in the dbchanger.php config file. Your config should look like this:

<?php

return [
    // this are the environments where dbchanger should work on. The active environment is fetched by .env file.
    'envs' => [
        'local'
    ],
    // this is the header parameter's name
    'headerParameter' => 'ApiTestIdentifier',
    // some infos about your db connection. Make sure this user is allowed to create databases.
    'connection' => [
        'host' => env('DB_HOST_TEST', 'localhost'),
        'user' => env('DB_USERNAME_TEST', 'homestead'),
        'password' => env('DB_PASSWORD_TEST', 'secret'),
        'prefix' => 'yourDbPrefix_'
    ]
];

Register the route-middleware in Kernel.php:

$routeMiddleware = [
  'db.changeable' => \Mintellity\DbChanger\DbChangerMiddleware::class,
  ...
]

How to use it

Now you are ready to use the DbChanger package. All you need to do is adding a middleware to the routes, where you want to listen for the db-change header-parameter. For example do this:

Route::group(['as' => 'api.', 'prefix' => 'api', 'namespace' => 'Api', 'middleware' => ['db.changeable']], function () {
// ...
});

If you send a request to one of this routes, you can add a header-parameter with the desired database to work on.

If you want to create a new database you have to add this route:

Route::get('buildDatabase/{databaseName}/{forceCreate?}', [
        'as' => 'apitest.buildDatabase',
        'uses' => '\Mintellity\DbChanger\DbChangerController@buildDatabase'
    ]);

So if you run http://yourpage.app/buildDatabase/testdatabase1/true in your browser, the system will create a new database called testdatabase1 (prefix can be defined in the config file). Validation for your database name is required|alpha_num|max:30|min:1. If you create a new database or you force create it, your seeds will be fired automatically.

About

Change your database with request header. Laravel 5.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages