<?php require_once './../vendor/autoload.php'; use Hal\Link; use Hal\Resource; // create the top level resource $purchase = new Resource(new Link('self', '/purchase/12234234'), 'purchase', array(new Link('payment', '/payment/9080'), new Link('contact', '/contact/8992'), new Link('shipping', '/shipping/1922')), array('items' => array(new Resource(new Link('self', '/item/12'), 'item', null, null, array('quantity' => 1)), new Resource(new Link('self', '/item/23'), 'item', null, null, array('quantity' => 2)), new Resource(new Link('self', '/item/45'), 'item', null, null, array('quantity' => 1)), new Resource(new Link('self', '/item/99'), 'item', null, null, array('quantity' => 3)))), array('date' => '2014-03-07')); print_r($purchase->toArray());
/** * Generate the URL for the read page for the given resource. * * @param Resource|int|string $resourceOrIdentifier * * @return string */ public function generateReadUrl($resourceOrIdentifier) { if ($resourceOrIdentifier instanceof Resource) { $data = $resourceOrIdentifier->toArray(); $id = $data['username']; } else { $id = $resourceOrIdentifier; } $route = $this->router->generate('ingewikkeld_rest_user_user_read', array('id' => $id), UrlGeneratorInterface::ABSOLUTE_PATH); return $route; }
<?php require_once './../vendor/autoload.php'; use Hal\Link; use Hal\Resource; // create the top level resource $resource = new Resource(new Link('self', '/customer/123'), 'customer'); // add some attributes to the resource $resource->addAttributes(array('firstname' => 'David', 'lastname' => 'White', 'age' => 32)); // add some embedded resources $address = new Resource(new Link('self', '/address/23987'), 'address'); $address->addAttributes(array('street' => 'A Road', 'town' => 'Harrogate', 'county' => 'North Yorkshire')); $resource->addEmbedded($address, 'addresses'); // add some links $orders = new Link('orders', '/customer/123/orders'); $payments = new Link('payments', '/customer/123/payments'); $resource->addLink($orders); $resource->addLink($payments); // print out the resulting array print_r($resource->toArray());
<?php // https://phlyrestfully.readthedocs.org/en/latest/halprimer.html require_once './../vendor/autoload.php'; use Hal\Link; use Hal\Resource; $resource = new Resource(new Link('self', 'http://example.org/api/user?page=3'), 'users', array(new Link('first', "http://example.org/api/user"), new Link('prev', "http://example.org/api/user?page=2"), new Link('next', "http://example.org/api/user?page=4"), new Link('last', "http://example.org/api/user?page=33")), array('users' => array(new Resource(new Link('self', 'http://example.org/api/user/mwop'), 'user', null, null, array('id' => 'mwop', 'name' => 'Matthew Weier O\'Phinney')), new Resource(new Link('self', 'http://example.org/api/user/mac_nibblet'), 'user', null, null, array('id' => 'mac_nibblet', 'name' => 'Antoine Hedgecock')), new Resource(new Link('self', 'http://example.org/api/user/spiffyjr'), 'user', null, null, array('id' => 'spiffyjr', 'name' => 'Kyle Spraggs')))), array('count' => 3, 'total' => 498)); $array = $resource->toArray(); echo json_encode($array, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);