定制 serebro/phalcon-rest 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

serebro/phalcon-rest

最新稳定版本:0.2

Composer 安装命令:

composer require serebro/phalcon-rest

包简介

phalcon rest api

README 文档

README

Phalconist

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,
	];
	
};

统计信息

  • 总下载量: 47
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 3
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-02