serebro / phalcon-rest
phalcon rest api
0.2
2015-04-25 12:18 UTC
Requires
- php: >=5.4
- ext-phalcon: >=1.3.2
Requires (Dev)
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-10-26 17:09:38 UTC
README
Configuration
Services (config/services.php)
$di = new \Phalcon\DI\FactoryDefault(); new \PhalconRest\ServiceProvider($di);
Router (config/routes.php)
$router = new \Phalcon\Mvc\Router(false); $router->removeExtraSlashes(true); $router->setDefaultNamespace('Controllers'); $rest = new \PhalconRest\Mvc\Router\RestGroup(); $rest->setNamespace('Controllers\Api') ->setPrefix('api/') ->setIdFilter('[0-9]+') ->initDefault(); $router->mount($rest); return $router;
Controller
ExampleController.php
<?php namespace Controllers\Api; class OrdersController extends \PhalconRest\Mvc\ControllerBase { public function initialize() { $this->rest->setViewsDir('api/'); } public function indexAction() { $this->view->total = Order::count(); $this->view->orders = Order::find(); } public function getAction() { $order_id = $this->dispatcher->getParam('id'); $order = Order::findFirst($order_id); if (!$order) { throw new \PhalconRest\Exception\NotFound(); } $this->rest->order = $order; $this->rest->pick('orders/_item'); } }
Response
<?php /* app/rest/orders/index.php */ return function ($params) { $items = []; foreach ($params['orders'] as $order) { $items[] = $this->partial('orders/_item', ['order' => $order]); } return [ 'results' => $items ]; };
<?php /* app/rest/orders/_item.php */ return function ($params) { return [ 'id' => $order->id, 'createdAt' => $order->created_at, 'userId' => $order->user_id, 'sum' => $order->sum, ]; };