2017 © Pedro Peláez
 

symfony-bundle rest-hook-bundle

A Symfony2 & 3 Bundle, that can easily convert any controller to a restful endpoint

image

sacheen/rest-hook-bundle

A Symfony2 & 3 Bundle, that can easily convert any controller to a restful endpoint

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 51 % Grown

The README.md

SDRestHookBundle

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)

Simple Example

<?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);
    }
}

/hello/{name}

This route will render the template Default::index.html.twig., (*3)

/api/hello/{name}

This route will output json by default, with no template required., (*4)

Installation

composer require "sacheen/rest-hook-bundle dev-master"

<?php
#AppKernel::registerBundles()
$bundles = array(
    // ...
        new SD\RestHookBundle\SDRestHookBundle(),
        new \JMS\SerializerBundle\JMSSerializerBundle()
    // ...
);

Config

#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)

JMSSerializerBundle

The Config allows for formats allowed by JMSSerializerBundle You can learn more about the bundle in its documentation., (*8)

Request Interceptor

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)

Exceptions

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)

Final

All controllers must return an array(), to render correctly., (*11)

Examples

    //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"}))

The Versions

04/06 2018

dev-master

9999999-dev https://github.com/sacheen/rest-hook-bundle

A Symfony2 & 3 Bundle, that can easily convert any controller to a restful endpoint

  Sources   Download

MIT

The Requires

 

by Sacheen Dhanjie

rest bundle symfony endpoint