I would like to design few different REST API, eg:
mobile.api.com - for mobile
client.api.com - for client side
admin.api.com - for admin side
However, these API might need to separate out to different server and using the reuse some code logic, since it is a same app, but process the output according different platforms.
My question is, how to make these different API to use the same code logic without duplicate the code if possible?
So far the solution I think of (hmm, not so suitable) is create a common API (where the same code logic sits here), each API will curl to this API (assume all server in same network)
EG:
if request a user details (where it is a common for each these API), mobile.api.com or admin.api.com will curl to common.api.com to get the user details, like common.api.com/users/userA , then process the output.
I hope there is a better architecture or solutions for this.
Thanks
I think just get one framework for example Yii
just creating module applications
all basic login will be in default module and we will have main models controller and others and modules will extends this classes
and in .htaccess file we will write some redirections
it just my openion. it have other variants also . . .
Related
I'm building an application in Symfony which can be identified by two distinct portions.
The first is a portion that handles front facing information to users
The second is the heart of the application that is driven by a number of CRUDs, management interfaces and controls to manage the front end
Currently, the two components sit with the following domains structure
Front end interface: www.example.com
Backend admin interface: www.example.com/app
Ideally, it would be nice to address the admin interface as admin.example.com.
I've thought about using vhost configs to create a reverse proxy from admin.example.com to www.example.com/app however I feel like this is a messy approach.
I've also explored the host option in the #Route annotation within Symfony, however this is also very messy as I need to define this (and a number of supporting default and requirement options) in each controller.
The core reason behind running the same application is that I'd like to have both halves of the application driven by the same database and the same Symfony entities. I understand that I can build two separate applications and this would solve my issue, however this would then create two separate sets of entities between the two projects and ultimately the potential for errors down the track. I would like to avoid having separate applications if I can.
Thanks in advance!
I've come up with a solution to something with this kind issue. Posting my own response in case any one comes across a similar issue and would like to know how I went about it.
As I mentioned there will be an admin control interface and a front end interface. They will both look very different however one will be more application based (admin) and one will be more information centric (front end website).
I have opted to use two separate applications. However making using of the FOSRestBundle I am exposing basic information in the form of a simple API that will be driven to drive a snappy and responsive front end using information from the back end component of the application. The front end doesn't need to be a Symfony application, just something that can render content from the API (either generated server side, or a secondary request post page load via JavaScript - whichever is more fitting).
While this isn't exactly what I was initially envisioning at first, I realise this is the better approach as it would have made the application much more bloated and difficult to maintain over time as the application grew. It will also be rather easy to create a number of simple tests to assure that the data provided by the API is as the front end expects, to avoid any issues as the back end is developed.
Hope this helps anyone!
When working on a personal project (a sports website) that I'm building in php with Codeigniter , would I gain anything from setting my application as RESTful ? I don't care about APIs (I'm the only one that's going to work on this project) . And even if I one day do care about making an API to the public , I don't see why I wouldn't be able to do it with Codeigniter's defaultbehaviour`.
Right now the routing is done like so
**base_domain/controller/controller_metho**d (as usual in CI)
Which seems just fine to me .
What or when will REST routing help me ?
If you are the only one using the code of your site you don't need REST. REST is mostly used when you want to allow clients (other websites or smartphones) to access data and maybe update or insert data into your database. That's when you would create API. It gives you full control to deliver data to clients, without giving them any snippet of your code or your database structure. Furthermore you can secure your code and data by implementing either PrivateKey/ PublicKey or username/password authentication.
You don't have to start implementing REST before you start building your site. You can always add REST service later when you are ready.
Take a look at this RESTful class by Phil Sturgeon made for Codeigniter 2
https://github.com/philsturgeon/codeigniter-restserver
And here is tutorial for the same thing
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/
This is a Homework task. It involves creating a simple DB and making CURL calls to the server to get results from the DB, accordingly I have a DBClass file with the required methods. I understand what REST architecture is in general, but I am kind of unable to put the pieces together. Here's what I have so far:
Model.class.php -> this is the Database class that instantiates connections to the DB and has methods that execute DB queries and return the result.
Simulator.php -> helper class, simulates HTTP requests (POST or GET only) to the localhost so my curl call is made to 'http://localhost/app/index.php'
index.php -> here is where I receive the CURL requests, in effect, I decode the HTTP requests to understand the request method, parameters, URI as such.
At this point, I am lost. Understandably a RESTful API request essentially is of the kind server/getMeMyBananas and getMeMyBananas is a DB method that looks for bananas for the user and returns the ID's. I am confused how this maps into the index.php file and the missing pieces.
Suggestions and links to awesome resources are most welcome. I am not interested in security or creating a state of the art Web service.
getMeMyBananas is an example of an RPC route
In REST, the four main HTTP verbs GET, POST, PUT and DELETE are the verbs to act upon a noun (a resource).
REST is not a standard. It's a loose recommendation on how to form an API for a remote system using HTTP as its foundation.
There's nothing to say that you can't design RPC-like routes in a REST API. People do it all of the time. It's just that you mainly use the verbs to create (POST), retrieve (GET), update (PUT) or delete (DELETE). That makes the acronym CRUD. So, with REST, you are able to cover a lot of scenarios in information exchange simply by sticking to CRUD.
So, you can start by designing your URLs (routes) to resemble nouns (resources) and build a switch case in PHP to switch on the HTTP verb. Keep in mind, there's nothing stopping from and there's nothing wrong with having RPC-like routes. In fact, you can't handle all cases with the simple REST CRUD scenario, so you'll have to handle cases that don't fit into that scenario with RPC-like routes.
See:
http://dojotoolkit.org/reference-guide/1.8/quickstart/rest.html
Later, if you are interested in a built out API in PHP, I built an API infrastructure and open sourced it. I'm not sure if it'll help you, but here it is:
https://github.com/homer6/blank_altumo
You can map any url to any path you want! For example, when using Apache you can use ModRewrite to turn http://ex.com/rest/bananas into http://ex.com/index.php?p1=rest&p2=bananas
From there you can now you can fetch your request parameters with the global variable get for example: $_GET["p1"].
I would suggest you to perform isset() test on those.
After that when you've got the data, I'd suggest to package it in JSON so almost any client can read it.
That's basically how I'd do it! If you've got more questions go ahead :)
My web development experience has mostly been setting up a CMS like Wordpress or Drupal and creating custom themes. Actually work in server-size coding has been very minimal. I've played around with php a little, trying to mod off of phpBB and beginning to learn some MVC work with CodeIgniter. Overall, this seems like a pretty big step forward, but it's something I need (I think) to do for a project I am working on.
Essentially what I want to do is have a service like Twitter of Facebook (not in the social networking sense); a user is able to log into the site and perform various operations, while also being able to use an android application that supports limited operations.
After some Googling and reading articles on the internet, it appears REST is the way to go. But I can't quite seem to grasp some of the technical details. I understand how the HTTP Request/Response works, but I don't know how I can code everything server side so that visiting example.com/item/1 will bring up the details of item 1 in the browser and can also perform a GET Request in my Android app so it can grab the details from the database and display in on the site.
Any suggested readings or some tips on how to execute this?
You can implement this using MVC. By default, have the controller ask the model for the details of the item, then pass the info to the view. Repeat this process for each type of request you want to accept such as POST, PUT etc., where you define a new function in the controller, ask the model to perform the corresponding database action, and return the response to the view.
There is helpful tutorial for getting a REST server up and running using CodeIgniter here
I have different questions about a full architecture idea. I hope someone with great experience could help me out because I am pretty much getting stuck in all possibilities.
I'm planning to rewrite a community website. Our customer wants to make use of native mobile apps in the future. So I will need to take this into account. Because of this I have decided to create a 100% REST API architecture based on the PHP framework Kohana. I have choosen Kohana because this makes scaling the internal API to a other server very easily without much extra effort. (Kohana threats internal url requests not as HTTP so there isn't much overhead in the beginning and can scale to HTTP with some minor code changes).
At first the API will be private, but later on we might make it public to let more services connect to us easily.
De basic REST structure is as follow:
/cats
/cats/1
/cats/1/custom
'custom' could be 'childs' for instance.
same goes for:
/ads
/bids
/users
/banners
etc..
These are perfect entities for the API because the mobile app will definitely use all this functionality.
So we can conclude the core of the website is REST. So basically I want to make the website a client of the API just like the native app in the future. This way maintenance seems much easier.
What worries me though is the fact that there is much more than this (managing uploaded files, invoicing, automails for invoicing, automails for ads and so on). Uploading files needs to go through the website to the API. Is this common practice? If I do not do this, the website would do upload logic, which makes the site no client anymore and the app itself. Hence the mobile app can't even upload and both API and website need to know the upload folder (duplicate logic).
I thought of creating the following modules:
community-api
community-website
Api seems to be the core then. But.... what about cronjobs etc? Actually they should not be part of the website, as this is just a 'client'. I feel they should interact directly with the model or API. So basically the API gets more like a gateway to the core and thought I need this:
community-core
Models
Cronjobs
Auto Mailings (part of cronjobs)
Invoices etc
community-api
Interact with models in core through HTTP
community-website
Website
Admin
The core cronjobs are a exception to the REST structure. They are the only one that can change data without going through the api. At least that was my idea because they belong in the core and API is on top of the core.
But by design that seems just wrong. Manipulating should only go through the API!
Alternative:
community-core
Models
community-api
Interact with models in core through HTTP
community business
Cronjobs
Auto Mailings (part of cronjobs)
Invoices etc
community-website
Website
Admin
This look better by design to me.
(source: mauserrifle.nl)
Main Questions
1)
Should cronjobs manipulate through the API or Core models?
2)
My invoice cronjob needs a template pretty much the style of main website of course. But if my cronjob is part of business or core it won't have knowledge of my main website. What makes sense to solve this?
3)
My website will be using mustache as a template engine. (both php and javascript can parse these templates). I thought using the api directly for ajax calls but then realized:
The site gets data from api, formats timestamps to dates (Y-m-d) for the template and then renders. If I let javascript call the api directly, javascript must have logic too (formatting). This is duplicate code! Feels like the only solution is calling the website for ajax (which calls the api and formats) and returns the formatted json. Am I right?
But.... simple calls like deleting a ad can go through the api directly (e.g. DELETE: /ads/1
I get a mix of calls....
Any better solution for this?
4)
Overall: Is my architecture too complex? Any alternatives I should consider?
I would love to hear your feedback!
Once I've heard that a good way to develop a web application is to develop an API-Centric Web Application. The thing is, to me, if you couple the main service to the public API, building an API-Centric application, you lose the whole point of developing a public API at all.
This doesn't seem logical to me.
Yes, the API and the website and what ever might come next are separate things and website should be a client to the API itself but since it will simplify things greate, I believe that you should RE-USE the domain-classes to build and base your web-site logic. This way you can use all the same code base and handle all your issues including ads, invoicing and of course file uploads with ease.
For the public API, it should be on a separate box if possible re-using the same domain classes but with different authentication methods so that whatever problem might occur, it won't affect the main service.
Your cron-jobs should only be used to fire the call through the API itself and those calls should be made on the main app (website through the API)
If you build your website without repeating-yourself, as in, using the same code as the base and wrapping the web-app around it, you won't have the issue raising in q#2.
Same thing applies for question number 3. If you wrap the website around the API, the website can use the api itself without being a separate entity.
Your architecture seems complex but if you do these things, it will be simple. ;-)
Good luck!
REST is just one way to initiate a request. Your core code that processes the request shouldn't be tightly coupled to the REST interface, or HTTP for that matter. I would advise designing your REST API as a simple mapper to an include file or something similar. Your cron could then bypass the whole REST API and just include the file directly. Separate the request interface from the code that does the actual processing.