dev-master
9999999-dev https://github.com/sacheen/rest-hook-bundleA Symfony2 & 3 Bundle, that can easily convert any controller to a restful endpoint
MIT
The Requires
by Sacheen Dhanjie
rest bundle symfony endpoint
Wallogit.com
2017 © Pedro Peláez
A Symfony2 & 3 Bundle, that can easily convert any controller to a restful endpoint
This bundle allows you to convert any controller, to become an api endpoint., (*1)
The purpose of this bundle is to allow re-use of controllers, and simplify creating API end-points., (*2)
<?php
class DefaultController extends Controller
{
/**
* @Route("/hello/{name}")
* @Route("/api/hello/{name}",name="_hello_api",defaults={"_format"="json"},requirements={"_method"="GET"}))
* @Template()
*/
public function indexAction($name)
{
return array('name' => $name);
}
}
This route will render the template Default::index.html.twig., (*3)
This route will output json by default, with no template required., (*4)
composer require "sacheen/rest-hook-bundle dev-master"
<?php
#AppKernel::registerBundles()
$bundles = array(
// ...
new SD\RestHookBundle\SDRestHookBundle(),
new \JMS\SerializerBundle\JMSSerializerBundle()
// ...
);
#app/config.yml
sd_rest_hook:
formats: [json,xml]
route_patterns: [/api/i,/ajax/i]
json_callback: json_callback
request_listener_priority: 100
The route_patterns allow for an array of regular expression, if a route matches the pattern, the kernel will intercept the response, and render it as the relevant end point format., (*5)
The json_callback option allows to specify the string, for a json_callback., (*6)
The request_listener_priority sets the priority for the intercepting the request., (*7)
The Config allows for formats allowed by JMSSerializerBundle You can learn more about the bundle in its documentation., (*8)
This feature allows the ability to send json data to the server, and it will convert the json to an http query string that your controller can understand., (*9)
There is a RestfulException Class, this allows you to set a data [array] variable that will then translate in the content of the response., (*10)
All controllers must return an array(), to render correctly., (*11)
//sample routes for get/put/post/delete
* @Route("/api/user",name="_user_get_api",defaults={"_format"="json"},requirements={"_method"="GET"}))
* @Route("/api/user",name="_user_put_api",defaults={"_format"="json"},requirements={"_method"="PUT"}))
* @Route("/api/user",name="_user_post_api",defaults={"_format"="json"},requirements={"_method"="POST"}))
* @Route("/api/user",name="_user_delete_api",defaults={"_format"="json"},requirements={"_method"="DELETE"}))
A Symfony2 & 3 Bundle, that can easily convert any controller to a restful endpoint
MIT
rest bundle symfony endpoint