jl.
jl.

Reputation: 2239

Structuring API and Controller in Laravel

I am trying to build a simple billing and invoicing system with Laravel, and intend to create an API for further development. I would like to find out how it is better to structure and house the controllers so that it will be neat and clean.

Which is a better option to go about for structuring and housing the API controller with normal UI controller within the same project?

1) Have them structured like:

/controllers
    |-- /API
          |-- InvoiceController.php
          |-- UserController.php
    |-- /UI
          |-- InvoiceController.php
          |-- UserController.php

2) Modular structure like:

/controllers
    |-- /API
          |-- InvoiceController.php
          |-- UserController.php

/modules (for normal UI controllers, etc)
    |-- /invoices
          |-- InvoiceController.php
    |-- /users
          |-- UserController.php

3) All in one structure like:

/controllers
    |-- InvoiceController.php
    |-- UserController.php
    |-- InvoiceControllerAPI.php
    |-- UserControllerAPI.php

Hopefully someone will be able to help me with the answers, or provide me with some advices and suggestions. Thanks.

Upvotes: 1

Views: 878

Answers (1)

Javi Stolz
Javi Stolz

Reputation: 4755

Your question is too broad. There are plenty of tutorial series around explaining how to build an API with Laravel.

My one piece of advice: Do not reinvent the wheel. Take a look at this excellent package: https://github.com/dingo/api. It allows you to build a flexible RESTful API that can be consumed both externally and by your own Laravel application. It has good documentation including a basic tutorial.

Upvotes: 1

Related Questions