<?php
// Fetch blog data from database then create blog resource
$blog = \Phpfox::getService("blog")->get($blogId);
$blogResource = new BlogResource($blog)Restful Mobile API design & Implementation
On this section, we will go through RESTful API design concepts, how to use and extend.
...
Code Block |
---|
{ "status": "success", "data": { "resource_name": "blog", "module_name": "blog", "title": "Et a dolor eum libero nostrum cumque.", "description": "Esse beatae voluptas officiis...", "module_id": "blog", "item_id": 0, "is_approved": true, "is_sponsor": false, "is_featured": false, "is_liked": false, "is_friend": false, "is_pending": false, "post_status": 1, "text": "Esse beatae voluptas officiis ratione ", "image": null, "statistic": { "total_like": 0, "total_comment": 0, "total_view": 1, "total_attachment": 0 }, "privacy": 0, "user": { "full_name": "Sheridan Hahn", "avatar": null, "id": 841 }, "categories": [ { "id": 4, "name": "Family & Home", "subs": null }, { "id": 9, "name": "Sports", "subs": null } ], "tags": [ { "tag_text": "tag me", "id": 3 } ], "attachments": [], "id": 1, "creation_date": "2016-06-21T04:53:48+00:00", "modification_date": null, "link": "http://localhost:7788/blog/1/et-a-dolor-eum-libero-nostrum-cumque/", "extra": { "can_view": true, "can_like": true, "can_share": true, "can_delete": true, "can_report": true, "can_add": true, "can_edit": true, "can_comment": true, "can_publish": false, "can_feature": true, "can_approve": false, "can_sponsor": true, "can_sponsor_in_feed": false, "can_purchase_sponsor": true }, "self": null, "links": { "likes": { "ref": "mobile/like?item_type=blog&item_id=1" }, "comments": { "ref": "mobile/comment?item_type=blog&item_id=1" } }, "feed_param": { "item_id": 1, "comment_type_id": "blog", "total_comment": 0, "like_type_id": "blog", "total_like": 0, "feed_title": "Et a dolor eum libero nostrum cumque.", "feed_link": "http://localhost:7788/blog/1/et-a-dolor-eum-libero-nostrum-cumque/", "feed_is_liked": false, "feed_is_friend": false, "report_module": "blog" } }, "error": null } |
Control Resource API Response
In the new Core Mobile API app, we define ResourceBase class for controlling resource output, mapping data fields and generating routes for Resource API.
Every new Resource class should extend from this base class and use the list of core resources for its relationship when building APIs. This way will help to reduce the code to build the API
Reusable core resources and common use cases
All resource classes are defined in the folder "PF.Site/Apps/core-mobile-api/Api/Resource" under name Space "\Apps\Core_MobileApi\Api\Resource"
Core's Resource | Description | Use Case |
UserResource | Presentation of a Phpfox's User |
|
TagResource | Tag feature in Phpfox |
|
NotificationResource | Core notification feature |
|
LikeResource | Core like feature |
|
CommentResource | Core comment feature |
|
FriendResource |
|
|
FileResource |
|
|
FeedResource |
|
|
AttachmentResource |
|
|
Reusable Core Objects
Unlike resource, Core Object is used to group related property properties of the Resource into an object. Following are a list List of reusable objects are listed in the following table:
Object Class | Use Case |
Image | Presentation as an Image of an Item (blog, marketplace listing) |
Privacy | Privacy of an item |
FeedParam | Feed parameter of an Item |
Statistic | Total Like, Total Comment, Total View of an Item |
Mapping property, related resource and object
To create a resource Resource object, we can fetch data from the database as an array Array and then populate to the Resource. Code example of Below is the sample code to creating a Resource from PHP arrayArray
Code Block |
---|
<?php |
...
// Fetch blog data from database then create blog resource |
...
$blog = \Phpfox::getService("blog")->get($blogId); |
...
$blogResource = new BlogResource($blog) |
We define some of convention way to mapping from a data source to resource's properties
Map resource's properties by naming convention:
If the data source has key same with the property name. It's auto mapped
Map with User Resource
If the data source has a set of "user_" prefix key and `user` property, It auto combine field to UserResource and Map to user property of the current resource
Manual mapping
Use can override or manual mapping use Setter and Getter methods. Setter method uses to control input data, Getter control the output.
All field query from the database has a String data type. But Native App requires to return exactly data type in JSON.
Override "loadMetadataSchema" method of resource to control the response data type.
Following is an example:
<?php
/* A lot of code above */
class PostResource extends ResourceBase
{
/* ... */
protected function loadMetadataSchema(ResourceMetadata $metadata = null)
{
parent::loadMetadataSchema($metadata);
$this->metadata
->mapField('title', ['type' => ResourceMetadata::STRING])
->mapField('description', ['type' => ResourceMetadata::STRING])
->mapField('item_id', ['type' => ResourceMetadata::INTEGER])
->mapField('module_id', ['type' => ResourceMetadata::STRING])
->mapField('is_approved', ['type' => ResourceMetadata::BOOL])
->mapField('is_sponsor', ['type' => ResourceMetadata::BOOL])
->mapField('is_featured', ['type' => ResourceMetadata::BOOL])
->mapField('is_liked', ['type' => ResourceMetadata::BOOL])
->mapField('is_friend', ['type' => ResourceMetadata::BOOL])
->mapField('post_status', ['type' => ResourceMetadata::INTEGER]);
}
}
...