PHPackages                             usox/sharesta - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. usox/sharesta

AbandonedArchivedLibrary[API Development](/categories/api)

usox/sharesta
=============

restful json api creation using strict hack

v3.5.0(6y ago)51.0k1MITHack

Since Aug 7Pushed 6y ago1 watchersCompare

[ Source](https://github.com/usox/sharesta)[ Packagist](https://packagist.org/packages/usox/sharesta)[ RSS](/packages/usox-sharesta/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (6)Versions (26)Used By (0)

Sharesta - Strict HAck RESTful Apis
===================================

[](#sharesta---strict-hack-restful-apis)

A micro framework to build simple and easy to use rest-like apis - written in strict hack ([Hack](http://hacklang.org)).

Usage
-----

[](#usage)

First, build some classes containing your logic..

```
final class HomeRoute implements \JsonSerializable {

    public function jsonSerialize(): string {
        return 'Welcome home';
    }
}

final class UpdateUserRoute implements \JsonSerializable {

    public function __construct(
        private int $user_id,
        private \Usox\Sharesta\RequestInterface $request
    ): void {
    }

    public function jsonSerialize(): bool {
        // do some magic, e.g. access the request body by $this->request
        return true;
    }

}
```

Now create a class containing your route configurations.

```
final class Routes implements Usox\Sharesta\RoutesInterface {

    public function registerRoutes(Usox\Sharesta\RouterInterface $router): void {
        $router->get(
            '/',
            (Usox\Sharesta\RequestInterface $request): \JsonSerializable ==> {
                return new HomeRoute();
            }
        );

        /**
         * Get variables from the path (e.g. `http://some.tld/users/123`)
         */
        $router->post(
            '/users/:id',
            (Usox\Sharesta\RequestInterface $request): \JsonSerializable ==> {
                return new UpdateUserRoute(
                    $request->getUriValues('id'),
                    $request->getRequestBody()
                );
            }
        );
    }
}
```

Setup sharesta, register your routes and let the application controller handle your requests.

```
