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.
Do you mean I should be writing the logic of fetching client identifier from url in Middleware and make it accessible globally?
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]);
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?
I've asked the same question on stackoverflow:
https://stackoverflow.com/questions/48705836/grouping-all-the-laravel-application-routes-using-dynamic-prefix
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community