A new framework for PHP 8.0 — Aphiria

Dave Young
5 min readDec 20, 2020
pronounced “Ah-fear-ee-ya”

Links: Documentation and source code

I already know what you’re thinking — another PHP framework? Why not use Laravel/Symfony/Slim/etc? Before, I dig into that, first, a bit of history.

How it all began

Back in February, 2014, I started work on what would eventually become my first PHP framework — Opulence. I still recall laying in bed, sick with a fever, and bored. I was reading about REST APIs — a new concept to me at the time. As so many developers do, I started off by trying to write my own libraries to help me understand REST and ORMs better. I never thought they’d see the light of day, but soon the company I was working at had a need for exactly the type of stuff I was building. And thus, Opulence was born. I was proud of how feature-full the framework was, but, over time, I realized I went too broad in my feature set rather than focusing on a few things and doing them exceptionally well. Trying to keep up with other full-stack frameworks on every front just wasn’t possible for an up-and-coming framework like Opulence. It was deflating.

Around that time, I changed jobs, and got into ASP.NET Core development. I was mesmerized by how great the developer experience was. Writing an API was trivial.

This sparked something in me to try and port what I learned in .NET over to PHP. In 2017, I started writing Opulence 2.0. My first action was to ditch the parts of Opulence that I just didn’t have the energy to support, eg its ORM, and instead put all my effort into building a great REST API framework that borrowed the best parts of ASP.NET Core. I poured many months into getting the syntax just right. It became clear, though, that Opulence 2.0 was such a huge departure from 1.0 that I could not in good conscience call it by the same name (think of the AngularJS 1 vs Angular 2 debacle). I decided to port over my work from Opulence 2.0 into a new GitHub repo, and thus Aphiria was born.

Thanks for the history lesson. Now what is Aphiria?

Aphiria is a simple, yet extensible REST API framework for PHP. It lets you focus on writing your application without bleeding into it, ie not having to extend base model classes. Like ASP.NET Core, Aphiria automatically performs content negotiation (ie figuring out what format and language a client is sending and is asking for back) of your POPOs. So, if a client sends XML in the request body but wants JSON back in the response body, Aphiria will automatically deserialize the request XML to the model you specify, and serialize your response model to JSON — all without any configuration on your end.

Let’s look at the Aphiria equivalent to the above .NET example:

In just 31 lines of expressive syntax, you can write a controller, configure dependency injection via a “binder”, and register global middleware. Aphiria is organized around the concept of “modules” (chunks of your domain) and “components” (pieces of your app that are shared across modules). Each module can register whatever it needs from components — binders, middleware, routes (if you’re not using attributes), exception-to-response mappings, validation rules, and console commands. You can even write your own components. Then, just plug-and-play modules into your app.

What features does Aphiria have?

Lots.

What features does Aphiria lack?

Aphiria is tightly focused on helping you write REST APIs. As such, it does not provide:

  • An ORM or database wrapper
  • A templating language
  • Custom authentication or authorization logic
  • Native PSR-7 support (learn why), but an adapter is provided

However, you can incorporate any 3rd party libraries you’d like into your Aphiria app to do these things.

Why should I use Aphiria as opposed to X?

Each framework is a tool with pros and cons. What I didn’t like about the current offerings are:

  • When a framework only works best when you’re all in on its stack
  • Inexpressive or verbose syntax to do the most simple things like configuring your app
  • A lot of magic, making it harder to grasp what’s happening under the hood

Obviously, I’m biased, but I feel that when it comes to REST API development, Aphiria provides the best developer experience of any of the popular PHP frameworks. I’ve gone into more detail comparing Aphiria with other frameworks here.

You’ve piqued my interest. How do I get started?

Aphiria provides a skeleton app to help you get up and running quickly via Composer. If you’d prefer to use individual libraries rather than the whole framework, you can. Interested in contributing? Read our contribution guidelines and check out some good issues to tackle. Have an idea for a cool feature? Create an issue for it. Want to ask for help or connect with others in the community? Check out our GitHub Discussions.

As with most other open source project authors, I have spent an incredible amount of free time building and maintaining Aphiria. If you’re able to spare a few bucks, I’ll always appreciate GitHub sponsorship. If you can’t, no worries.

Parting words

I’d like to thank my wife Lindsey for putting up with my countless late nights, buried in a computer working on something she didn’t understand. She saw how happy it made me, and was gracious enough to support my passion. Aphiria is a labor of love for me — I make no money off of it. I hope that the years I’ve put into building it are apparent, and that they make your life easier as a PHP developer. If not, open a pull request :)

--

--

Dave Young

Author of Aphiria, director of engineering at Higi