1. Go to this page and download the library: Download aeris/zend-rest-module library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
aeris / zend-rest-module example snippets
class AnimalRestController extends AbstractRestfulController {
public function get($id) {
// Grab the animal entity from the database
$animal = $this
->animalRepository
->find($id);
// Serialize the animal into a JSON string
$jsonString = $this
->serviceManager
->get('the_jms_serializer_service_I_configured_on_my_own')
->serialize($animal, 'json');
// But JsonModel expects an array, so we need
// deserialize the JSON string back into a php array.
$serializedData = json_decode($jsonString);
return new JsonModel($serializedData);
}
}
class AnimalRestController extends AbstractRestfulController {
public function get($id) {
return $this
->animalRepository
->find($id);
}
}
class AnimalRestController extends AbstractRestfulController {
public function create($data) {
// Deserialize the raw JSON data into
// a MyApp\Entity\Animal object
$animal = $this->serviceManager
->get('Aeris\ZendRestModule\Serializer')
->deserialize($data, 'MyApp\Entity\Animal');
$this->saveToDb($animal);
return $animal;
}
}
class AnimalRestController extends AbstractRestfulController {
public function update($id, $data) {
$animal = $this->entityManager
->getRepo('MyApp\Entity\Animal')
->find($id);
// Update the existing animal from the
// PUT data
$this->serviceManager
->get('Aeris\ZendRestModule\Serializer')
->deserialize($data, $animal);
$this->saveToDb($animal);
return $animal;
}
}
use JMS\Serializer\Annotation as JMS;
class Animal {
/**
* @JMS\Groups({"animalSummary", "animalDetails"})
* @var int
*/
public $id;
/**
* @JMS\Groups({"animalSummary", "animalDetails"})
* @var string
*/
public $name;
/**
* @JMS\Groups({"animalDetails"})
* @var string
*/
public $species;
/**
* @JMS\Groups({"animalDetails"})
* @var string
*/
public $color;
/**
* @JMS\Groups({"dates"})
* @var
*/
public $birthDate;
}
class Animal {
/**
* @JMS\Type("DateTimeTimestamp")
*
* @var \DateTime
*/
public $birthDate;
}
// zend-rest.config.php
'zend_rest' => [
'errors' => [
[
// The `error` can be a Zend\Mvc error string
// or the class name of an Exception
'error' => \Zend\Mvc\Application::ERROR_ROUTER_NO_MATCH,
// HTTP response code
'http_code' => 404,
The `error.code` property of the JSON response object
'application_code' => 'invalid_request',
The `error.details` property of the JSON response object
'details' => 'The requested endpoint or action is invalid and not supported.',
],
[
'error' => 'MyApp\Exception\RidiculousAnimalException',
'http_code' => 418,
'applicationCode' => 'ridiculous_animal_error',
'details' => 'The animal you have requested is too riduculous for our web service.',
'on_error' => function(RestErrorEvt $evt) {
// This is a good place to log errors
$exception = $evt->getError();
error_log($exception);
// You can also modify the error view model
$viewModel = $evt->getViewModel();
$errorObject = $viewModel->getVariable('error');
$errorObject['animal'] = $exception->getAnimal();
$viewModel->setVariable('error', $errorObject);
}
],
[
// You should always
class AnimalRestController extends AbstractRestfulController {
public function get($id) {
if ($id === 'narwhal') {
throw new RidiculousAnimalException("narwhal");
}
return $this->animalRepo->find($id);
}
}
[
'zend_rest' => [
// Required.
'cache_dir' => __DIR__ . '/cache'
// Set to true to disable caching.
'debug' => false,
// See "Exception Handling" documentation
'errors' => [
[
'error' => \Zend\Mvc\Application::ERROR_ROUTER_NO_MATCH,
'http_code' => 404,
'application_code' => 'invalid_request',
'details' => 'The requested endpoint or action is invalid and not supported.',
'on_error' => function(RestErrorEvt $evt) {
error_log("Someone requested an invalid endpoint.");
}
]
],
'controllers' => [
// See "Serialization Groups" documentation.
'serialization_groups' => [
'MyApp\Controller\UserRest' => [
'get' => ['details', 'timestamps'],
'getList' => ['summary']
]
]
],
// See "Serializer" documentation
'serializer' => [
'handlers' [
'\Aeris\ZendRestModule\Service\Serializer\Handler\DateTimeTimestampHandler',
],
'naming_strategy' => '\Aeris\ZendRestModule\Service\Serializer\Naming\IdenticalPropertyNamingStrategy',
'object_constructor' => 'Aeris\ZendRestModule\Serializer\Constructor\InitializedObjectConstructor',
'enable_max_depth' => true,
]
]
]
use Aeris\ZendRestModule\View\Annotation as View;
class AnimalRestController extends AbstractRestfulController {
/**
* @View\Groups({"animalDetails", "dates"})
*/
public function get($id) {
return $this->entityManager->find($id);
}
/**
* @View\Groups({"animalSummary"})
*/
public function getList() {
return $this->entityManager->findAll();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.