I'm writing an api using Laravel 4 where a portion of the URL will determine what database the data will be pulled from. There will be a large number of databases each with the same schema, for example:
mystuff_arizona
mystuff_florida
...
This is a given (a requirement I have to work with).
So the following urls (to 'show' a resource by id)...
www.mysite.com/arizona/api/v1/items/9
www.mysite.com/florida/api/v1/items/9
can use the same controller, but I need to add some code in Laravel (in a base controller or 'before' function) to setup the correct database connection depending on the URL.
I've been working with something like this:
Route::group(['prefix' => '{state}/api'], function($state=null)
{
Route::group(['prefix' => 'v1'], function()
{
Route::resource('items', 'ItemsController');
});
});
But, in my controller, I need to add the $state parameter to the show() method, although I'm not going to use it there.
//public function show($state,$id) // <<-- this works
public function show($id) // <<-- this fails
{
$record = Item::findOrFail($id);
return Response::json( $record->toArray()) );
}
My questions are:
1) Is there a way to access the $state parameter from the route before 'show' is called (either in the base controller or in the Controller's 'before' method)?
2) Is there a way to tell show() and update() resource methods to forget about the $state parameter?
I could use native PHP to do this I'm sure, but I'm wondering if Laravel has some pre-built functionality to offer here.