I am using Laravel 4 to create APIs namespaced by accountname
of each of my customers. Each customer has their own, identical database. So Foocorp should make api calls that look like this:
Barcorp api calls look like this
I must include the account name in the URL for business/branding reasons, so I cannot eliminate this parameter from the URL routes.
Here is a filter that I was using to attempt to pull out the account name from the route, verify it is active, and point to their database. I hoped to remove the accountname
parameter so that I could write all my controller functions to not include an $accountname
parameter for all of them.
Route::filter('accountverification', function()
{
$route = Route::getCurrentRoute();
$params = $route->getParameters();
$accountName = $params['accountname'];
// verify account with this name exists and set up DB connection to point to their database
// ...
unset($params['accountname']);
$route->setParameters($params);
});
Here's my route group that uses the filter:
Route::group(array('prefix' => '{accountname}', 'before' => 'accountverification'), function() {
Route::get('users/{id}', 'UsersController@getShow')
->where(array('id' => '[0-9]+'));
});
The problem is that removing the parameter in the filter does not have any effect when controller/function is called. In the UsersController::getShow function the first parameter is always the accountname
from the group prefix.
Is there a way for me to include a variable/parameter in all my routes that I can do something with before the request is dispatched, which won't be passed to the function?