-
Notifications
You must be signed in to change notification settings - Fork 25
Basic Usage
Shashank Jain edited this page Oct 1, 2016
·
2 revisions
The package provides its own model, controller and router.
-
Model: Extend your eloquent model from
ApiModel
class instead ofModel
class:
class User extends ApiModel
{
...
}
-
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;
}
-
Router: All the routes that are part of the API should be defined through
ApiRoute
class. You can continue to use regularRoute
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.