CodeIgniter Forums

Full Version: Writing an API (for mobile app) on top of a web application?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have built my web application on Codeigniter 3. Now, I want to build mobile apps on top of it.

What's the best way to structure Codeigniter to do this?

I am thinking of having an "api" folder within the controller folder.

Anyone has experience doing this?
(01-04-2016, 11:43 AM)komirad Wrote: [ -> ]I have built my web application on Codeigniter 3. Now, I want to build mobile apps on top of it.

What's the best way to structure Codeigniter to do this?

I am thinking of having an "api" folder within the controller folder.

Anyone has experience doing this?

How are you building your mobile app?
(01-04-2016, 02:05 PM)albertleao Wrote: [ -> ]
(01-04-2016, 11:43 AM)komirad Wrote: [ -> ]I have built my web application on Codeigniter 3. Now, I want to build mobile apps on top of it.

What's the best way to structure Codeigniter to do this?

I am thinking of having an "api" folder within the controller folder.

Anyone has experience doing this?

How are you building your mobile app?

I haven't actually started building it. So far, I have only done some research on the tools available (such as PhoneGap)
Yes, have an API Controller and have it spit out JSON for the most part (nice and easy to parse in pretty much any mobile app)
Generally, I just prefer to have my web application be as mobile-friendly as possible, rather than building specific apps for mobile platforms. However, when I absolutely need an API for an external application, I find it best to sit down and design the API with an eye towards the existing web application's structure. In some cases, a specific API end-point makes sense; in other cases, the API will largely be made up of additions to or extensions of my existing URL structure.

In either case, the ease of building a separate controller specifically for this purpose will depend on how easily I can use my existing resources from that controller. If all of my existing controllers are relatively thin and all of my business logic is built into my models or libraries, then it's relatively straight-forward to add a new controller which loads the existing libraries and models to build an API to work with the data.

On the other hand, if you need to work within the existing controllers, you may find it easier to use those controllers within your API. You might add new methods to existing controllers for the API end-points, and possibly refactor existing methods to extract the business logic (which you may want to move to libraries or models at some point).

No matter which method you use for the code structure, you can still use your routes config to define the external API, pointing specific API end-points at different controllers, or making routes which might normally point to different controllers route instead to your API controller.