Welcome Guest, Not a member yet? Register   Sign In
[SOLVED]Web and mobile via same backend
#1

(This post was last modified: 10-19-2015, 02:39 AM by PureTryOut.)

EDIT: Solution found thanks to rtorralba and mwhitney. Going to make use of a REST api which I'm going to build in seperate controllers.


I'm not sure how to start with this, maybe one of you has a suggestion.

Basically I'm going to create a website, and a mobile application (Android, iOS, doesn't matter), which both have to talk via the same PHP backend. This backend interacts with the database.
So I want the frontend (so the website + mobile application) to send some sort of "command" to the backend. The backend then does something depending on the command it gets, and returns a value (I guess JSON would be the easiest here).

Should I create a seperate project for the backend, and call it from the frontend via a CURL call, or something like that? Or should I create the backend in the same project as the frontend, but seperate the controllers in different folders, and call the backend directly? I would think the latter is better for performance and security, but I don't know how to call one controller from the other, if that is even possible.

Not sure if I'm really clear so an image to describe what I mean:
[Image: 5NSTNC.svg]
Reply
#2

(10-16-2015, 04:57 AM)PureTryOut Wrote: I'm not sure how to start with this, maybe one of you has a suggestion.

Basically I'm going to create a website, and a mobile application (Android, iOS, doesn't matter), which both have to talk via the same PHP backend. This backend interacts with the database.
So I want the frontend (so the website + mobile application) to send some sort of "command" to the backend. The backend then does something depending on the command it gets, and returns a value (I guess JSON would be the easiest here).

Should I create a seperate project for the backend, and call it from the frontend via a CURL call, or something like that? Or should I create the backend in the same project as the frontend, but seperate the controllers in different folders, and call the backend directly? I would think the latter is better for performance and security, but I don't know how to call one controller from the other, if that is even possible.

Not sure if I'm really clear so an image to describe what I mean:
[Image: 5NSTNC.svg]

Hi,

I recommend you the following structure:


- controllers
  - backend/
  - frontend/
  - api/
- models
- views
  - backend/
  - frontend/

With this structure you can share and reuse the models, this is interesting because a lot of data will be the same, then you don't repeat the code.

For api you can't extend from REST_Controller from codeigniter-restserver that is very easy to use and powerful.
Greetings.
Reply
#3

Sorry you misunderstood, the backend is in this case not an administration panel of some sorts, but more like a server, a daemon. The user can't access it, it will not load any views. It gets a command from the mobile app or website, and returns a value. Not any more then that.

I just have it so the mobile app won't directly access the database, and I don't have to write the same database code twice (for web and app).

The backend is more like the API you described I guess.
Reply
#4

(This post was last modified: 10-16-2015, 08:12 AM by rtorralba.)

(10-16-2015, 07:49 AM)PureTryOut Wrote: Sorry you misunderstood, the backend is in this case not an administration panel of some sorts, but more like a server, a daemon. The user can't access it, it will not load any views. It gets a command from the mobile app or website, and returns a value. Not any more then that.

I just have it so the mobile app won't directly access the database, and I don't have to write the same database code twice (for web and app).

The backend is more like the API you described I guess.


Yes, backend is a database adminstration application. If you don't need it you can extract it from your structure.

You want retrieve web data across api? You will go to use angularjs or some other ajax framework to your public web layer? If not the frontend controllers can manage data across models directly.

Structure with php frontend:
- controllers
  - frontend
  - api (rest api for mobile data manage)
- models
- views

Structure with rich client frontend:

- controllers
  - api (rest api for mobile and web javascript data manage)
- models
- views (at views you can put your rich client framework)

Of course you should to have a public assets folder for js, css, images...
Greetings.
Reply
#5

If you want the web front- and back-ends completely separate, just write the back-end as an API using REST or whatever works for you. This could be a CodeIgniter application or whatever makes sense to you. Use the same API for both the web and mobile versions, since the API should just manage data.

For the web front-end, I would probably focus on building a base model that works well with your API, since you're not going to be using the database in the front-end application. Where another application would load the database and use $this->db, your web front-end will be loading CURL or Guzzle and calling the API. So, you would probably build a library to access your data, possibly using a set of methods similar to those used by CI's database library, but managing the data through the API via HTTP requests. You may also want to consider caching the data on the front-end server, which would probably also be a good thing to handle primarily in your library and base model.

You may want to take caching into consideration when defining your API, too. For instance, the client might optionally supply the timestamp of its cached data when requesting that data from the server, so the server could just confirm that the data hasn't changed, or supply the data if it has changed (or if no timestamp was supplied). Of course, you'll also want to make sure those timestamps are all generated with the same timezone information, which means they should probably be supplied by the back-end server, as well.

The mobile application would just call the same API using whatever tools are appropriate for your target device.

The biggest thing to remember, though, is that your API should usually be handling very specific data for each call. If you setup the web front-end to display three distinct pieces of data, you're going to make three API calls to get it. Once you have a working base, maybe you would go back and add a special API call to combine those three calls to optimize your application, but more than likely that's just going to be additional complexity with limited benefit, and you should make decisions like that based on testing your application, rather than assuming that one API call will be faster than three (after all, maybe 2/3 of the data is cached under most conditions).
Reply
#6

Thanks to both of you, I had not heard of RESTful, but it seems like it is exactly what I want. Thanks for your answers, I'll look into it!
Reply
#7

(10-19-2015, 12:07 AM)PureTryOut Wrote: Thanks to both of you, I had not heard of RESTful, but it seems like it is exactly what I want. Thanks for your answers, I'll look into it!

You are welcome. Rate it if the reply helped you. 
Greetings.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB