Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Tutorial 2: Show list of Posts on App's home screen

The previous tutorial we have added a new menu item to the main menu, but it has no action when clicks on. This tutorial will create new screens for Post app and display a list of posts when clicking on the menu.
Following are step by step of the tutorial

Step 1: Create the primary app Resource

Each app requires a primary Resource to control the app's settings. In this case, we will create Post Resource as the primary Resource. Following is minimal code to make it works.
PostResource.php
<?php
namespace Apps\Posts\Api\Resource;
use Apps\Core_MobileApi\Api\Resource\ResourceBase;
class PostResource extends ResourceBase
{
public $resource_name = "post";
public $module_name = "post";
public $title;
public $description;
public $text;
}
 
The sample code above we create PostResource class extends from ResourceBase class. Override $resource_name and $module_name properties to classify with other apps. All other class's properties consider as fields stores Posts in the database.

Step 2: Implement resource API service

Create PostApi class extends from AbstractResourceApi class and implements MobileAppSettingInterface interface, and all require methods need to be implements will appear. Then implement two methods
Method PostApi::findAll() will handle get all posts restful API. Method PostApi::getAppSetting() register setting of the Post app to the Native Mobile App. Minimal code show as below:
PostApi.php
<?php

namespace Apps\Posts\Api\Service;

use Apps\Core_MobileApi\Adapter\MobileApp\MobileApp;
use Apps\Core_MobileApi\Adapter\MobileApp\MobileAppSettingInterface;
use Apps\Core_MobileApi\Api\AbstractResourceApi;
use Apps\Posts\Api\Resource\PostResource;
class PostApi extends AbstractResourceApi implements MobileAppSettingInterface
{
public function findAll($params = [])
{
$posts = [
new PostResource([
'post_id' => 1,
'title' => "Post's title example",
'description' => "Post's description example",
'text' => "Post's description example"
])
];
return $this->success($posts);
}
public function getAppSetting($param)
{
$app = new MobileApp('post' ,[
'title'=> 'Posts',
'home_view'=>'menu',
'main_resource'=> new PostResource([])
]);
return $app;
}
/* All other methods let emplty for implements later */
}
 
We have finished implement two primary classes of Post application. Now need to register to the Phpfox system

Step 3: Register Post App to Phpfox System

Create new hooks mobile_api_routing_registration.php with the following code
mobile_api_routing_registration.php
<?php
/**

  • Define RestAPI services
    */
    $this->apiNames['mobile.post_api'] = \Apps\Posts\Api\Service\PostApi::class;
    /**
  • Register Resource Name, This help auto generate routing for the resource
  • Note: resource name must be mapped correctly to resource api
    */
    $this->resourceNames['post'] = 'mobile.post_api';
     
    Register PostApi service in start.php of the App
    PostApi.php
    <?php
    namespace Apps\Posts;
    use Phpfox;
    Phpfox::getLib('module')
    ->addServiceNames([
    'mobile.post_api' => Api\Service\PostApi::class,
    ]);
     

    Step 4: Test the integration

    If all the steps above are complete, a new app screen on the mobile app was registered and connected with the Menu Item. The new app screen called main Post's app screen, and it combines with API get all posts to get data via restful API and show on the screen
    You need clear Phpfox's cache and re-open the Native Mobile App, click on the Posts menu item to see the result.

    Organizing plugin's source code

    In this tutorial, we but all source code that use to implement API integration within the API directory and Under API namespace

     
     
     
  • No labels