PHP routing library for fast API development
Tags: php, showdev, opensource
Published: 2021-04-09 16:07:51

---
title: PHP routing library for fast API development
published: true
description: I've created and published my custom routing library written in vanilla PHP.
tags: php, showdev, opensource
//cover_image: https://direct_url_to_image.jpg
---

{% github gigili/PHP-routing no-readme %}

# What it is?

PHP Routing is a custom written routing library that was inspired by the [Slim framework](https://www.slimframework.com/). It was written as a small and lightweight alternative to it as I needed to use something with as little overhead as possible (not that Slim has a big foot print, hence the name Slim :D ). The first version of the library was consisting of a single PHP class that was able to handle static and some basic dynamic routes and had a semi working support for adding and using middlewares.

At first that was more than enough for my use case and the library was left at that, but recently I started rewriting one of my personal projects [Did You Buy It?](https://github.com/gigili/did-you-buy-it-api) live on my [Twitch](https://twitch.tv/gacbl/) channel and decided I wanted to use PHP for it, as the original API was developed using Node and Express.

## What does it do?

This library allows for quick API development with support for both static and dynamic routes. It has support for adding prefixes to routes (ability to group them), add middlewares to be executed before the routes are processed. I'm also working on some new features but if you feel like something is missing or would like to see it implemented do leave feedback on the project.

A small sample on how to add your own routes:
```php
$routes = new Routes;

$routes->add('/', function() { echo "Hello world"; }, Routes::GET); // If the request method is not specified the GET method is used by default

$routes->add(
    '/users/{int:userID}/posts/{bool:isPublished}',
    function(Request $request, int $userID, bool $isPublished){
        //Do something
    },
    [Routes::GET, Routes::POST]
);

$routes->route();
```

## Why did I build it?

At my day job we needed to develop an API and I've never really tried to build one using vanilla PHP, but instead used frameworks such as [CodeIgniter](https://codeigniter.com/) or [Slim](https://slimframework.com) or just a simple file that would mimic the functionality of an API but not be a proper one. Also we had a requirement that we needed to have full control over the code so we were not supposed to use any of the pre built solutions because of that requirement.

I thought I would look into how to build my own routing library and implement it for our use case. I started of by taking a look at the source code of Slim framework but also just by looking around the web for examples of what other people had done before. One of the video tutorials that really inspired me and I used it as guide while developing this was the one from Codecourse on youtube on how to build your own MVC app.

{% youtube OsCTzGASImQ %}

## Where to find it ?

You can find the source code of the library on my [GitHub](https://github.com/gigili), but also on the [Packagist](https://packagist.org/packages/gac/routing) website and use it as a composer package.

{% github gigili/PHP-routing no-readme %}

## Stay in touch

If you want to reach out to me, you can do so on my [Twitter](https://twitter.com/Gac_BL), [GitHub](https://github.com/gigili) or [Twitch](https://twitch.tv/gacbl)

{% twitter 1380094882031800324 %}