I have just started tinkering with Laravel (PHP newbie alert) and I have a doubt on how to implement my REST JSON APIs on this framework.
The desired URL path should be:
http://api.mysite.com/APIKEY/FUNCTION/(ARGUMENTS)*
But what is the best way to do it?
I did not find any enough explanatory guide, because it is assumed that each feature (authentication, search, and so on) are on different routes.
At the moment I managed to create migrations and models for the needed data and I also set a route this way:
Route::get('/{apikey}/{function}/{arg1}/{arg2}/{arg3?}',
//The first two arguments are mandatory, the 3rd optional
function($apikey,$function,$arg1,$arg2)
{
return Response::json(array(
'status'=>'200'),
200);
})
->where(array('function'=>'[A-Za-z]+'));
This should be the correct action flow, but I have some doubts.
- Check that apikey is valid, if not return a 404 json response. How do I call such function, where do I define it?
- If key check is successful, understand which function is needed (should I use a switch construct or is there a better way to implement this, like a route group? In all examples with
Route::group
there is a static prefix, but here the prefix is apikey, a variable) - Return data - if available - getting it from the database. I suppose that for each function i should code a specific controller that gets data from the database using the
Models
.