Welcome to Abdul Malik Ikhsan's Blog

Auto add _links property of HAL Resources into all api service in Apigility

Posted in Tutorial PHP, Zend Framework, Zend Framework 2, Zend Framework 3 by samsonasik on July 10, 2017

If you want to have the _links property value to HAL Resource in apigility api service, for example:

    "id": 1,
    "name": "Abdul Malik Ikhsan",
    "_links": {
        "self": {
            "href": "http://api.dev/user/1"

you can do manually in every api service:

use ZF\ContentNegotiation\ViewModel;
use ZF\Hal\Entity as HalEntity;
use ZF\Hal\Link\Link;

// ...

    public function userAction()
        $halEntity = new HalEntity([
             'id' => 1,
             'name' => 'Abdul Malik Ikhsan',   

        $link = $halEntity->getLinks();
                  'rel' => 'self',
                  'url' => $this->getRequest()->getUriString(),

        return new ViewModel([
            'payload' => $halEntity,

// ...

You can eliminate that by apply via EventManager’s Shared Manager which attach to Zend\Mvc\Controller\AbstractActionController on dispatch event, like below:

namespace Application;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\MvcEvent;
use ZF\Hal\Link\Link;
use ZF\Hal\Plugin\Hal;

class Module
    public function onBootstrap(MvcEvent $e)
        $app       = $e->getApplication();
        $sharedEvm = $app->getEventManager()->getSharedManager();

        $sharedEvm->attach(AbstractActionController::class, 'dispatch',
            function($event) use ($sharedEvm) {

                $uri = $event->getRequest()->getUriString();

                $sharedEvm->attach(Hal::class, 'renderEntity', function($event) use ($uri) {
                                    'rel' => 'self',
                                    'url' => $uri,



    public function getConfig() { /* */ }

On above code, we attach ZF\Hal\Plugin\Hal on renderEntity event which get the ZF\Hal\Entity object from ZF\ContentNegotiation\ViewModel payload property, and apply Link into it via ZF\Hal\Link\Link::factory().

Now, you can eliminate unneeded repetitive codes in all every api services.

Done 😉

Tagged with: , ,

One Response

Subscribe to comments with RSS.

  1. Vladimir said, on November 17, 2019 at 11:19 am

    I am just strugling with apigitlity and HAL links in collection entities. I have a collection of entities where in the metadatamap I have defined a link with route and route parameters. This works and each entity within collection has this link properly renderd. Now I can’t find a way how to make the route parameter value set from one of the entity atributes. If I set it via metadatamap its static for all entities. For example the entity is ticket and the ticket has owner identified by owner id. That is why I wold like to render for every ticket link ‘owner’ with value set to Ticket->getOwnerId() this is all within a collection of tickets. Please do you have any idea how to do that? Thanks in advance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: