1

I want to route requests according to request methods (GET, POST, PATCH, DELETE, PUT)

how do I do this in dingo?

I have tried this, but it isn't working.

$api = app('Dingo\Api\Routing\Router');
$api->get('users', 'UserController@index', ['only' => [ 'index']]);
Nagendra Rao
  • 7,016
  • 5
  • 54
  • 92

1 Answers1

2

Routing works the exact same way as it does in Laravel/Lumen (depending on where you're using this pacakge).

Your code is partly correct. When you use $api->get you only need to provide the URI it responds to and the action that should be taken. The third parameter is not required.

So it should look like this:

$api->get('users', 'UserController@index');

Now when you browse /api/users it will look for the UserController and use the index method. It should be worth noting that you may need to include the full namespace to the controller, e.g., App\Http\Controllers for a fresh Laravel install.

You can utilize the other HTTP verbs as well, much like in Laravel/Lumen.

$api->post('users', 'UserController@create');

Finally, you can utilize resource controllers, which is what the third parameter is for in your example.

$api->resource('users', 'UserController', ['only' => ['index', 'create']]);

Note: All of these must be contained within an API version group, e.g., $api->version('v1', function ($api) { });

Jason Lewis
  • 18,537
  • 4
  • 61
  • 64