|
1 | | -# Symphony Api Framework |
| 1 | +# RESTful API Framework for Symphony CMS |
2 | 2 |
|
3 | | -This framework adds a Json renderer to Symphony and helps to quickly build an API with a, event driven, controller interface. |
| 3 | +JSON renderer and event driven controller interface for Symphony CMS designed to quickly build a RESTful APIs. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +This is an extension for Symphony CMS. Add it to your `/extensions` folder in your Symphony CMS installation, then enable it though the interface. |
| 8 | + |
| 9 | +### Requirements |
| 10 | + |
| 11 | +This extension requires the **[Symfony HTTP Foundation](https://github.com/symfony/http-foundation)** (`symfony/http-foundation`) and **[Symphony PDO](https://github.com/pointybeard/symphony-pdo)** (`pointybeard/symphony-pdo`) to be installed via Composer. Either require both of these in your main composer.json file, or run `composer install` on the `extension/api_framework` directory. |
| 12 | + |
| 13 | + "require": { |
| 14 | + "php": ">=5.6.6", |
| 15 | + "symfony/http-foundation": "^3.0@dev", |
| 16 | + "pointybeard/symphony-pdo": "~0.1" |
| 17 | + } |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +This extension has two parts: The JSON renderer, and the Controller event. |
| 22 | + |
| 23 | +### JSON Renderer |
| 24 | + |
| 25 | +Any page with a type `JSON` will trigger the new JSON renderer. This automatically converts your XML output into a JSON document. This includes output from any events. |
| 26 | + |
| 27 | +### Controller Event |
| 28 | + |
| 29 | +Use the `API Framework: Controller` event to listen for PUT, POST, PATCH and DELETE requests. To create your own controller, make a folder called `controllers` in your `/workspace` directory. |
| 30 | + |
| 31 | +A controller will respond to the 4 methods (PUT, POST, PATCH and DELETE) via a same named public method. E.g. to respond to a PUT request, create a method called 'put' in your controller like so |
| 32 | + |
| 33 | + public function put(Request $request, Response $response) |
| 34 | + { |
| 35 | + ... |
| 36 | + } |
| 37 | + |
| 38 | +Modify `$response` to set return values and status code. E.g.: |
| 39 | + |
| 40 | + $response->setStatusCode(Response::HTTP_OK); |
| 41 | + |
| 42 | +Lastly, call the render method (which is inherited) to generate the output. E.g. |
| 43 | + |
| 44 | + return $this->render($response, ['data' => 'some output']); |
| 45 | + |
| 46 | +To actually use a controller, name it the same as a top level page. E.g. if you had a page called "entry", and you wanted to provide PUT, POST, PATCH and DELETE functionality, name your controller class "ControllerEntry" and the file `ControllerEntry.php` |
| 47 | + |
| 48 | +Here is an example of a completed controller: |
| 49 | + |
| 50 | + <?php |
| 51 | + |
| 52 | + use Symfony\Component\HttpFoundation\Request; |
| 53 | + use Symfony\Component\HttpFoundation\Response; |
| 54 | + use Symphony\ApiFramework\Lib\AbstractController; |
| 55 | + use Symphony\ApiFramework\Lib\Traits; |
| 56 | + |
| 57 | + final class ControllerExample extends AbstractController{ |
| 58 | + |
| 59 | + use Traits\hasEndpointSchemaTrait; |
| 60 | + |
| 61 | + public function execute(){ |
| 62 | + // Optional. Add any code here, such |
| 63 | + // as including extension or autoloaders. |
| 64 | + // This method is automatically invoked |
| 65 | + // anytime this controller is going to be used. |
| 66 | + } |
| 67 | + |
| 68 | + public function post(Request $request, Response $response) |
| 69 | + { |
| 70 | + $data = $request->request->all(); |
| 71 | + |
| 72 | + try{ |
| 73 | + // do some work here |
| 74 | + $response->setStatusCode(Response::HTTP_CREATED); |
| 75 | + $response->headers->set( |
| 76 | + 'Location', "some/path/to/resource" |
| 77 | + ); |
| 78 | + return $this->render($response, [ |
| 79 | + 'data' => [ |
| 80 | + 'id' => $idOfNewResource, |
| 81 | + ], |
| 82 | + 'status' => $response->getStatusCode(), |
| 83 | + 'message' => "Item was successfully created." |
| 84 | + ]); |
| 85 | + |
| 86 | + } catch(\Exception $ex) { |
| 87 | + // handle errors here. |
| 88 | + } |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + public function put(Request $request, Response $response) |
| 93 | + { |
| 94 | + $someEntryId = (int)Frontend::instance() |
| 95 | + ->Page() |
| 96 | + ->Params()['preference-id']; |
| 97 | + |
| 98 | + $data = $request->request->all(); |
| 99 | + $output = []; |
| 100 | + |
| 101 | + try{ |
| 102 | + // do some work here with "someEntryId" |
| 103 | + $output = [ |
| 104 | + 'message' => "Entry successfully updated.", |
| 105 | + 'status' => Response::HTTP_OK |
| 106 | + ]; |
| 107 | + |
| 108 | + } catch(Exceptions\ModelEntryNotFoundException $ex) { |
| 109 | + $output = [ |
| 110 | + 'message' => "Entry not found.", |
| 111 | + 'status' => Response::HTTP_NOT_FOUND |
| 112 | + ]; |
| 113 | + } |
| 114 | + |
| 115 | + $response->setStatusCode($output['status']); |
| 116 | + return $this->render($response, $output); |
| 117 | + } |
| 118 | + |
| 119 | + public function patch(Request $request, Response $response) |
| 120 | + { |
| 121 | + // Sometimes it is okay just to use the |
| 122 | + // PUT code to handle PATCH requests also |
| 123 | + return $this->put($request, $response); |
| 124 | + } |
| 125 | + |
| 126 | + public function delete(Request $request, Response $response) |
| 127 | + { |
| 128 | + $someEntryId = (int)Frontend::instance() |
| 129 | + ->Page() |
| 130 | + ->Params()['some-id']; |
| 131 | + |
| 132 | + $output = []; |
| 133 | + |
| 134 | + try{ |
| 135 | + // do some work here using "someEntryId" |
| 136 | + $output = [ |
| 137 | + 'message' => "Entry successfully deleted.", |
| 138 | + 'status' => Response::HTTP_OK |
| 139 | + ]; |
| 140 | + |
| 141 | + } catch(Exceptions\ModelEntryNotFoundException $ex) { |
| 142 | + $output = [ |
| 143 | + 'message' => "Entry not found.", |
| 144 | + 'status' => Response::HTTP_NOT_FOUND |
| 145 | + ]; |
| 146 | + } |
| 147 | + |
| 148 | + $response->setStatusCode($output['status']); |
| 149 | + return $this->render($response, $output); |
| 150 | + } |
| 151 | + } |
0 commit comments