PHP code example of codag / restfabrication-bundle
1. Go to this page and download the library: Download codag/restfabrication-bundle 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/ */
codag / restfabrication-bundle example snippets
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Codag\RestFabricationBundle\CodagRestFabricationBundle(),
);
}
<service id="acme_api.domain_manager.myresource" class="%codag_rest_fabrication.domain_manager.default.class%">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument>AcmeApiBundle:Myresource</argument>
</service>
public function getAction()
{
return $this->get('acme_api.domain_manager.myresource')->findAll();
}
public function deleteAction(Request $request, $id) {
$manager = $this->get('acme_api.domain_manager.myresource');
$obj = $manager->find($id);
if(!$obj){
throw new ResourceNotFoundException('Myresource', $id);
}
$manager->delete($id);
return $this->routeRedirectView('myresource_all', array(), Codes::HTTP_NO_CONTENT);
}
<service id="acme_api.form_handler.myresource" class="%codag_rest_fabrication.form_handler.create_form.class%">
<argument type="service" id="acme_api.domain_manager.myresource" />
</service>
public function postAction(Request $request){
try {
$form = $this->createForm(new MyresourceType(), new Myresource(), array('method' => 'POST'));
$new = $this->get('acme_api.form_handler.myresource')->handle($form, $request);
return $this->routeRedirectView('myresource_get', array('id' => $new->getId()), Codes::HTTP_CREATED);
}catch (InvalidFormException $exception) {
return $exception->getForm();
}
}
public function putAction(Request $request, $id){
try {
$manager = $this->get('acme_api.domain_manager.myresource');
$formHandler = $this->get('acme_api.form_handler.myresource');
if (!($object = $manager->get($id))) {
$statusCode = Codes::HTTP_CREATED;
$form = $this->createForm(new MyresourceType(), new Myresource(), array('method' => 'POST'));
} else {
$statusCode = Codes::HTTP_NO_CONTENT;
$form = $this->createForm(new MyresourceType(), $object, array('method' => 'PUT'));
}
$object = $formHandler->handle($form, $request);
return $this->routeRedirectView('myresource_get_all', array('id' => $object->getId()), $statusCode);
} catch (InvalidFormException $exception) {
return $exception->getForm();
}
}
if($form->isValid()){
...
}
throw new InvalidFormException('Invalid submitted data', $form);
try {
...
} catch (InvalidFormException $exception) {
return $exception->getForm();
}
throw new ResourceNotFoundException('Myresource', $id);
bash
php composer.phar update codag/restfabrication-bundle