PHPackages                             rodriguezric/corto - 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. [Framework](/categories/framework)
4. /
5. rodriguezric/corto

ActiveLibrary[Framework](/categories/framework)

rodriguezric/corto
==================

A small framework based on functions.

v0.0.0(4y ago)15MITPHP

Since Jul 6Pushed 4y ago2 watchersCompare

[ Source](https://github.com/rodriguezric/Corto)[ Packagist](https://packagist.org/packages/rodriguezric/corto)[ RSS](/packages/rodriguezric-corto/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (2)Used By (0)

Corto
=====

[](#corto)

Synopsis
========

[](#synopsis)

Corto is a microframework for building REST APIs. The philosophy for this framework is built around using functions, singletons and static calls to objects to replace dependencies on global variables. This framework does not attempt to make a large, object-oriented solution, rather, an opinionated approach to chaining functions from requests.

Classes
=======

[](#classes)

There are two classes in this framework: `Request` and `Pipe`. Everything else is a function.

Request
-------

[](#request)

The `Request` object is a singleton that extracts the URI and HTTP method from the `#$_SERVER` global variable, and converts `php://input` into an array. It expects the input to be `JsonSerializable`.

Pipe
----

[](#pipe)

The `Pipe` is an invokable object is has a `mixed` property called `$value`. Invoking the object returns a `callable` that mutates `$value` and then returns `$this`. This allows to create a succinct syntax for piping functions on a value:

```
(new Pipe(10))
    (fn($x) => $x * 2);

// returns 20
```

Functions
=========

[](#functions)

Corto is built around a workflow of functions:

- `request` for retrieving request data.
- `route` for listening to HTTP requests, parses paths and returning responses.
- `response` for setting the HTTP code and returning JSON.

request
-------

[](#request-1)

Calling `request()` returns the `Request` singleton. When creating a function `request()->input` is used for accessing the data sent.

route
-----

[](#route)

The `route` function is the most complex in the framework. It’s a curried function that starts by describing the HTTP method:

```
route('GET');
```

This returns a function that accepts two parameters: the path and the callback. Paths are strings that are compared to the URI of the request. There are two types of paths: `literal paths` and `variable paths`.

### Literal Paths

[](#literal-paths)

Literal paths have no variables included and are compared directly with the URI. An example of a literal path is `/path/to/resource`.

```
route('GET')('/path/to/resource', ...);
```

### Variable Paths

[](#variable-paths)

Variable paths include curly braces to represent values to path to the callback function. Variable paths are converted to regular expressions before being compared to the URI. An example of a variable path is `/path/to/resource/{variable}`

```
route('GET')('/path/to/resource/{variable}', ...);
```

**Note** the word between the curly braces is meaningless, any word between the braces can be used to represent that value. If there are more than one variable included in a variable path they are passed to the callback **in order**.

response
--------

[](#response)

The response function is a curried function that starts with the HTTP response code. The callable returned by the function expect either `array` or `JsonSerializable` data. It sets the HTTP response code and then converts the data into JSON, echoing the JSON format. After the echo is complete, it `exits`.

This function is tightly coupled to JSON output and also terminates the program. I am comfortable with these decisions, keeping the framework strictly defined for one purpose: `delivering JSON responses based on requests`.

Example
=======

[](#example)

Here is an example of how to implement the features described in the `Functions` section. This would go in your public-facing script, like `index.php`

```
