francisdesjardins / webservice-rest-php
为 Fat-Free-Framework 设计的简单 REST 辅助工具
1.0.0
2016-03-07 20:37 UTC
Requires
- bcosca/fatfree-core: 3.5.1
- league/event: 2.1.2
- league/oauth2-server: 4.1.5
- myclabs/php-enum: 1.4.1
- psr/log: 1.0.0
Requires (Dev)
- phpmd/phpmd: @stable
- phpunit/phpunit: 4.4.*
- squizlabs/php_codesniffer: 2.*
This package is not auto-updated.
Last update: 2024-09-25 23:20:29 UTC
README
为 Fat-Free-Framework 设计的简单 REST 辅助工具
进行中
这里没有什么太多可以说的...它是一个正在进行中的项目...您需要自己找出如何使用它...但是不要担心朋友们,在tests中有一些示例可以帮助您。
快速简单的示例
index.php
require_once '../../../vendor/autoload.php'; use FrancisDesjardins\WebService\Rest\Responder\Encoder\NoopResponderEncoder; use FrancisDesjardins\WebService\Rest\Responder\ErrorResponder; use FrancisDesjardins\WebService\Rest\Utility; /** @var Base $fw */ $fw = Base::instance(); //! load globals $fw->config('../app/globals.ini'); //! load globals (OS specific) if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { $fw->config('../app/globals.windows.ini'); } else { $fw->config('../app/globals.linux.ini'); } //! load custom sections $fw->config('../app/custom.ini'); //! load mappings $fw->config('../app/maps.ini'); //! global error handler if (!Utility::instance()->debug()) { $fw->set('ONERROR', function (Base $fw) { Utility::instance()->flushOutputBuffer(); $responder = new ErrorResponder($fw); $responder->respond(new NoopResponderEncoder()); }); } //! run it! $fw->run();
一个映射
namespace FrancisDesjardins\WebService\Rest\Maps; use FrancisDesjardins\WebService\Rest\AbstractRest; use FrancisDesjardins\WebService\Rest\AuditTrait; use FrancisDesjardins\WebService\Rest\DynamicData; use FrancisDesjardins\WebService\Rest\ErrorEnum; use FrancisDesjardins\WebService\Rest\Event\ErrorEvent; use FrancisDesjardins\WebService\Rest\FatFreeFrameworkTrait; use FrancisDesjardins\WebService\Rest\Responder\Encoder\GzipResponderEncoder; use FrancisDesjardins\WebService\Rest\Responder\JSONResponderTrait; use FrancisDesjardins\WebService\Rest\Security; use FrancisDesjardins\WebService\Rest\SecurityRule\LocalhostSecurityRule; use FrancisDesjardins\WebService\Rest\Test\Models\Result\Ok; use FrancisDesjardins\WebService\Rest\UtilityTrait; class Example extends AbstractRest { use AuditTrait; use FatFreeFrameworkTrait; use JSONResponderTrait; // every VERB respond as application/json use UtilityTrait; // do something before routing public function before() { // ex: allow only localhost access Security::instance()->addRule(new LocalhostSecurityRule()); // ex: audit a token if (!self::audit()->uuid(self::fff()->get('PARAMS.uuid'))) { // generate a 500 error in the headers and return {error: 5001} to the client self::dispatchEvent(new ErrorEvent(ErrorEnum::ERROR500(), 5001)); } } // do something on 'GET' public function get() { // and reply $this->setData(new DynamicData(['property1' => 'value1'])); } // do something on 'POST' public function post() { // retrieve the data; eventually it will be automatic $data = self::utility()->convertUnicodeToUTF8(self::fff()->get('BODY')); if ($data !== '') { // do something } // and reply $this->setData(new Ok()); } // do something after routing public function after() { // we want every VERB to GZIP the response; can also be set per VERB $this->setEncoder(new GzipResponderEncoder()); } }