In the previous article we showed how to create a new RESTful API service using Apigility, now we want to interact with an existing PHP application to publish an API.

The goal is to interact with the business logic of an existing PHP application to retrieve the data and expose via API.

Of course, this part depends on the existing PHP application. For instance, if you have an MVC application that's very simple, you need only to consume the Model part of your application. For a legacy PHP application this can be more complicated but it's always possible. 

When you create a REST API, Apigility creates 4 PHP files. For instance, imagine that we created a User service, the files generated by Apigility are (under the folder /module/Test/src/Test/V1/Rest/User, where Test is the name of the API used in the previous article):

- UserEntity.php

- UserCollection.php

- UserResource.php

- UserResourceFactory.php

The Entity is the object model of the User service, it should contains all the data related to the user. The Collection is a set of entities. The Resource is the component that manages the API call (GET, POST, PUT, PATCH, DELETE) and the ResourceFactory is the class that create an instance of the Resource.

We can use the ResourceFactory component to inject the dependencies of the existing PHP application. For instance, imagine that we have a User.php class that implements the business logic of the application we can inject it to the UserResource:

class CatalogResourceFactory
    public function __invoke($services)
        $mapper = $services->get("PHPApplicationUser");
        return new UserResource($mapper);

The PHPAppliationUser is the name of instance of the User component of the existing app. Here, we are using the ServiceManager of ZF2 to set/get the object to be shared in the APIs. We can set this object in the Module.php file (under the folder /module/Test/src/Test), using the getServiceConfig() method:

public function getServiceConfig()
    return array(
        "factories" => array(
          "PHPApplicationUser" =>  function ($sm) {
            // here you need to instantiate the User class of the existing PHP application
			// includes and dependencies should be added here
			// $sm is the instance of the ZF2 Service Manager used by Apigility
            return new User();

In this way we can consume the existing PHP code of the application to extract the data to pass to the API. For instance, we can retrieve the data of a specific user and return a UserEntity in the UserResource:

protected $mapper;

public function __construct($mapper)
    $this->mapper = $mapper;

public function fetch($id)
	// image we have a getUser() method to retrieve the user"s data
	$data = $this->mapper->getUser($id);
	// we need to return an Entity
    return new UserEntity($data);

The fetch() method manages the API URL "GET /user[:/user_id]". In this way, you can support all the other HTTP methods.

Additional materials:

- Create a RESTful API with Apigility

- Adding Apigility to an existing ZF2 project

- Create RESTful API for Symfony2 with Apigility

Enrico Zimuel

Senior Software Engineer at Zend Technologies , Website , Git home page , @ezimuel , Linkedin profile
Developer since Texas Instruments Ti99/4A, TEDx and international speaker, open source contributor (co-author of Apigility and ZF2), curios about computer and art, co-founder of the PHP User Group of Torino (Italy), ex basketball player.

All articles by Enrico Zimuel


comments powered by Disqus


Follow Us