Skip to content

Basic Usage

Shashank Jain edited this page Oct 1, 2016 · 2 revisions

The package provides its own model, controller and router.

  1. Model: Extend your eloquent model from ApiModel class instead of Model class:
class User extends ApiModel
{
   ...
}
  1. Controller: Extend your controller from ApiController class and define the property $model to contain the class name of the model this controller is associated with:
class UserController extends ApiController
{
   protected $model = User::class;
}
  1. Router: All the routes that are part of the API should be defined through ApiRoute class. You can continue to use regular Route class for all other routes.
ApiRoute::group(
    [
        'middleware' => ['api'],
        'namespace' => 'App\Http\Controllers'],
        function () {
            ApiRoute::resource('user', 'UserController');
    }
);

That's it! Your API endpoint /api/v1/user will now work. All the REST methods - index, store, show, update and delete - work out of the box.

This is a bare minimum setup. There are various ways you can modify default functionality, which is described in later chapters.

Clone this wiki locally