1

I am building a RESTful api using Laravel. I am confused on how to do the routing.

I have the following api controller

class APIController extends BaseController{

    public function sendMsg($authid, $roomid, $msg){

    }

    public function getMsg($roomid, $timestamp){

    }
}

The URL format I want this to be accessible looks like this: http://example.com/api/{functionName}/{parameter1}/{parameter2}/.../

Here, in the first parameter, I will have the function name which should map to the function in the controller class and following that the parameters the controller needs.

For example
To access the sendMsg() function, the url should look like this:
http://example.com/api/sendMsg/sdf879s8/2/hi+there+whats+up

To access the getMsg() function, the url should look like http://example.com/api/getMsg/2/1395796678


So... how can I write my routes so that it can handle the dynamic number and different parameters need?

I can write one route for each function name like so:

Route::get('/api/sendmsg/{authid}/{msg}', function($authid, $msg){
    //call function...
});

and same for the other function. This if fine but is there a way to combine all function to the APIController in one route?

1 Answer 1

2

Yes, you can combine all the function to your APIController in one route by using a resourceful controller which is best suited for building an API:

Route::resource('api' ,'APIController');

But, technically, it's not one route at all, instead Laravel generates multiple routes for each function, to check routes, you may run php artisan routes command from your command prompt/terminal.

To, create a resourceful controller you may run the following command from your command line:

php artisan controller:make APIController

This will create a controller with 6 functions (skeleton/structure only) and each function would be mapped to a HTTP verb. It means, depending on the request type (GET/POST etc) the function will be invoked. For example, if a request is made using http://domain.com/api using GET request then the getIndex method will be invoked.

public function getIndex()
{ 
    // ...
}

You should check the documentation for proper understanding in depth. This is known as RESTful api.

Sign up to request clarification or add additional context in comments.

8 Comments

Your answer is interesting but it doesn't answer my question, My question is how do I map an undefined set of parameters to functions. Like function1 needs 3 parameter and function2 needs 5 parameters. How do I do that?
You can use api/{param1?}/{param2?} and these will be optional and your function must use a default value for each parameters like function f($p1 = null, $p2 = null) is that you asked for ?
Well, that could work but say if I have 4 function each with 4 unique parameters, that would be 16 parameters!. Maybe I wasn't clear enough before, is there a function that gets all the parameters in an associative array or something along those lines?
You can use Route::current()->parameters() to get all the parameters in an array.
Yes that is exactly what I am looking for! Now one last thing. If I have a url like this http://domain.com/api/getMsg/param1/param2/param3/ how do i make a route that responds to any url with getMsg as the first parameter
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.