Introduction

What Is It?

Aphiria is a suite of decoupled libraries that help you write expressive REST APIs without bleeding into your code base. Let's look at an example controller.

final class UserController extends Controller
{
    public function __construct(private IUserService $users) {}

    public function createUser(Credentials $creds): IResponse
    {
        $user = $this->users->createUser($creds->email, $creds->password);

        return $this->created("/users/{$user->id}", $user);
    }

    public function getUser(int $id): User
    {
        return $this->users->getUserById($id);
    }
}

In createUser(), Aphiria uses content negotiation to deserialize the request body to a Credentials object. Likewise, Aphiria determines how to serialize the User from getUser() to whatever format the client wants (eg JSON or XML). This is all done with zero configuration of your plain-old PHP objects (POPOs).

To use these endpoints, we'll need to include a binder so that an instance of IUserService can be injected into the controller, and we need to define our routes. You can centrally configure all of these parts of your user domain in a module.

final class UserModule implement IModule
{
    use AphiriaComponents;

    public function build(IApplicationBuilder $appBuilder): void
    {
        $this->withBinders($appBuilder, fn () => [new UserServiceBinder])
            ->withRoutes($appBuilder, function (RouteCollectionBuilder $routes) {
                $routes->post('/users')
                    ->mapsToMethod(UserController::class, 'createUser');

                $routes->get('/users/:id')
                    ->mapsToMethod(UserController::class, 'getUser');
            });
    }
}

Modules allow you to configure each of your business domains individually, making it trivial to plug-and-play new ones. For example, you can specify binders, routes, validators, and other components that your domain needs.

Hopefully, these examples demonstrate how easy it is to build an application with Aphiria.

Getting Started

To get up and running, follow our installation guide to create a skeleton app that uses Aphiria. Then, learn how to define some routes, create some controllers, and configure your dependencies. From there, you can browse the docs in any order you choose, although the order they're listed in might be the best way to read them.

Aphiria uses a GitHub project for keeping track of new features, bug fixes, and roadmapping. To learn more about the direction of the framework, check out the project.

Another PHP Framework?

Great question. The idea for Aphiria was conceived after using ASP.NET Core. Its expressive syntax, intuitive models, and simple configuration inspired me to see if I could find these things in a PHP framework. I looked at frameworks, and usually found at least one major problem with them all:

There is a general trend towards having separate API and front end codebases. Full stack frameworks have their place, but I felt like this was becoming a less and less common way to write websites - most are JavaScript-powered UIs calling APIs. So, I focused on what I know best - building REST APIs. I spent months sketching out the ideal syntax, laboring over how to provide the best developer experience possible. Once I had the syntax down, I worked backwards and started implementing it over the course of a few years. It wasn't easy, but I can honestly say I am very proud of it. I hope you enjoy it, too.