1

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.

1 Answer 1

1

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.

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

1 Comment

Sorry, should have made it clearer, am interested to find out the structuring, how people actually put together the API and controllers, are they in the same structure within the controller dir, or is it modular, etc? Most of the tutorials I could find were independent, either how to build restful API or how to build a simple system with Laravel (or maybe I am not searching correctly?). I wonder how the industry actually do it for their applications, and the pros and cons of modular and such structures, etc. Thanks for the dingo api link. This is great for all to be included in the Route.

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.