Zend Server Gateway is a lightweight web services gateway based on Zend Framework 2, which allows for the mapping of web service routes to various controller/actions of the web services. Zend Server Gateway is responsiblefor authentication, validation, filtering, and routing for RPC and RESTful APIs used in CCM projects.
The routing configurations are mapped into config/gateway.xml; the routes and configurations can be managed using the gateway editor interface provided in Zend Studio.
In this task, we will be creating a simple search interface for searching the existing customer records by name using the following steps:
1. We will need to create a search function in the CustomerRepository model (MyMobileService\src\MyCompany\Model\CustomerRepository.php):
public function getSearch($query)
{
$where = new \Zend\Db\Sql\Where();
$where->like('name', "%$query%");
return $this->customerTable->select($where)->toArray();
}
2. Add a new action in RpcController (MyMobileService\src\MyCompany\ Controller\RpcController.php); this will handle the web service request:
public function getSearchCustomersAction ($query)
{
$cr = new CustomerRepository(); return $cr->getSearch($query);
}
3. In the gateway editor, create a new RPC service; set the following options:
‰ URL: /search
‰ Method: GET
‰ Request Parameters(Add): Name – query; Source – Route
‰ Handler Method: MyCompany\Controller\RpcController::getSear chCustomersAction
4. You can test the RPC service by right-clicking on the service and choosing Test Service. On the right-hand side you will be presented with an interface to provide test input and validate the service response:
5. In the mobile GUI editor, create a new page searchCustomers, and add the following elements:
‰ Text Box: custsearchinput
‰ Button: searchbutton
‰ List View: custlistview
6. In the binding section of the Search button, bind the button with the GET / search:query() web service. Map the custsearchinput textbox to the query route parameter in the data section. This actionwill bind the search text to the query route parameter. Note that the query route parameter is already mapped to getSearchCsutomerAction.
7. Modify the onGetSearchquery JavaScript method in MyMobileApp/www/js/ my.js to handle the RPC response:
function onGetSearchquery(response) {
// TODO Custom logic to handle server response customers = response;
var newCustomers = '';
$.each(customers, function(index, item) { newCustomers += '<li data-theme="">'
+ '<a href="#page2?empId=' + index
+ '" data-transition="none">' + item.name + '</a>' + '</li>';
});
$('#custlistview li[role!=heading]').remove();
$('#custlistview').append(newCustomers).listview('refresh');
}
8. Make sure that you link the Search page from the index page using a button.
9. Now run the project in native mode; you will be able to see the search page, like the one shown in the following screenshot:
What just happened?
We have now created new web services for the existing cloud-connected mobile application and have tested the mobile app in a native emulator. With Zend Studio 10, you can see the simplicity in building mobile apps which are supported by web services running on the cloud.
Q1. Which of the following platforms are supported in Zend Studio 10 for native mobile application development?
1. Android
2. Firefox OS
3. MeeGo
4. Brew
Q2. Which of the following web services are not supported by Zend Server Gateway for building cloud-connected mobile applications?
1. RPC
2. SOAP
3. REST
Summary
Cloud-connected mobile applications are a great step by Zend towards enabling PHP developers to build and support mobile apps using the cloud platform. With CCM, Zend is offering an extremely robust, yet simple-to-use platform for building these applications.