PHP code example of matmar10 / rest-api-bundle

1. Go to this page and download the library: Download matmar10/rest-api-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/ */

    

matmar10 / rest-api-bundle example snippets



    // in app/AppKernel.php
    public function registerBundles()
    {
        $bundles = array(
            ...
            new Matmar10\Bundle\RestApiBundle\Matmar10RestApiBundle(),
        );

    }




    namespace Acme\DemoBundle\Controller;

    use Matmar10\Bundle\RestApiBundle\Annotation\Api;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;

    /**
     * @Api("json")
     */
    class RestApiBundleTestJsonController extends Controller
    {
        public function getArrayAsJsonAction()
        {
            return array(1,2,3,4);
        }

        /**
         * @Api("xml")
         */
        public function getArrayAsXmlAction()
        {
            return array(1,2,3,4);
        }
    }
}


    @Api("json")
    public function getPersonAction()
    {
        $person = new Person();
        $person->setName("Matthew J. Martin");
        $person->setAge(28);
        return $person;
    }

// results in: {"name":"Matthew J. Martin","age":28}



    @Api("json", responseCode=201)
    public function createPersonAction()
    {
        $person = new Person();
        // process the request, persist the entity, and then return it
        return $person;
    }