Releases: serebro/phalcon-rest
Releases · serebro/phalcon-rest
0.2
0.1
Phalcon Rest 0.1
Configuration
View Engine
$di->set('jsonEngine', function($view, $di) {
$engine = new \PhalconRest\Mvc\View\Engine\Json($view, $di);
$engine
->setJsonEncodeOptions(JSON_UNESCAPED_UNICODE | JSON_BIGINT_AS_STRING | JSON_PRETTY_PRINT)
->setJsonpContentType('application/javascript')
->setJsonContentType('application/json')
->setCallbackParamName('json_callback');
return $engine;
});
Rest View
$di->set('view', function(){
$restView = new \PhalconRest\Mvc\RestView();
$restView->setViewsDir(APP_PATH . '/responses/');
$restView->registerEngines(['.php' => 'jsonEngine']);
return $restView;
});
Router
$di->set('router', function () {
$router = include APP_PATH . '/config/routes.php';
$rest = new \PhalconRest\Mvc\Router\Rest();
$rest
->setNamespace('\Controllers\Api')
->setPrefix('/api')
->setIdFilter('[0-9]+')
->init()
->mountTo($router);
return $router;
});
Controller
ExampleController.php
class OrdersController extends \Phalcon\Mvc\Controller {
public function indexAction() {
$this->view->total = Order::count();
$this->view->orders = Order::find();
}
public function getAction() {
$order_id = $this->dispatcher->getParam('id');
$this->view->order = Order::findFirst($order_id);
$this->view->pick('orders/item');
}
}
Json Response Views
<?php
/* /responses/orders/index.php */
$items = [];
foreach ($orders as $order) {
$items[] = $this->partial('orders/_item', ['order' => $order]);
}
return [
'meta' => (object)[
'number' => count($items),
'total' => $total,
],
'results' => $items
];
<?php
/* /responses/orders/item.php */
return $this->partial('order/_item', ['order' => $order]);
<?php
/* /responses/orders/_item.php */
return [
'id' => $order->id,
'createdAt' => $order->created_at,
'userId' => $order->user_id,
'sum' => $order->sum,
];