Support the ongoing development of Laravel.io →
Laravel Configuration
Last updated 1 year ago.
0
moderator

You could use a Middleware to change the database config depending the client portal identifier. Depending if you have all the configurations in your database file or that it is flexibel you need to decide how you want to validate it.

0

Do you mean I should be writing the logic of fetching client identifier from url in Middleware and make it accessible globally?

0
moderator

A middleware can be for all the routes. And you can change the default connection on the fly (only for that request)

$clientIdentifier = 'client1234';
config(['database.default' => $clientIdentifier]);

Last updated 6 years ago.
0

My Web.php file is as below..

/* 
   Application URL : https://xyz.com/ClientPortal123/user/edit/12 
   Where ClientXXX  is dynamic 
*/

Route::pattern('ClientPortal','^ClientPortal([0-9]+)?');
Route::prefix('/{ClientPortal}')->group(function () {
    Route::get('/user/edit/{id}', 'UserController@edit');
});

I've created Middleware with below code written in it..

public function handle($request, Closure $next)
{
    $database_name = strtolower($request->ClientPortal).'_db';
    config(['database.connections.mysql.database'=>$database_name]);
    config(['app_settings.client'=>$request->ClientPortal]);
    return $next($request);
}

And it's working fine but previously I used to access $id in edit function as a parameter directly public function edit($id)

but now $id return the value of {ClientPortal} every time but If i access $id like

public function edit(Request $request){
    $id = $request->id;
} 

It's working fine so I'm not sure if this happened because I'm using dynamic prefix for grouping all the routes? And now for in every route() function which I have used in blade files for url generation I have to pass the second parameter ie. value of **ClientPortal **

{{ route('register',['ClientPortal'=>config('app_settings.client')]) }}

is this write implementation?

Last updated 6 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.