GithubHelp home page GithubHelp logo

willitscale / streetlamp Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 1.0 164 KB

A HTTP router application wrapper.

Home Page: https://packagist.org/packages/willitscale/streetlamp

License: MIT License

PHP 100.00%
data-mapper http php php82 router hacktoberfest composer composer-package php83

streetlamp's Introduction

Streetlamp

GitHub Workflow Status (with branch) Packagist License Packagist Version GitHub last commit

Table of Contents

1. Introduction

Streetlamp is a simple routing library that allows you to quickly prototype APIs. This library was built around the basic concepts of annotative routing, commonly found in Java libraries such as JAX-RS and Spring. Although the way it works is inspired from the aforementioned Java libraries, it has a slightly unique implementation more fitting the PHP language.

2. Prerequisites

To keep up with modern standards this library was built using PHP 8.2 and therefore will only run in said environment or greater. If there is enough demand I may be willing to retrofit back to PHP 8, but as it's built with attributes it can't go back any further. Finally, this project requires composer and the PSR-4 Autoload standard.

3. Setup

3.1. Installing the library

Simply include Streetlamp in your project with the composer command:

composer require willitscale/streetlamp

3.2. Application Wrapper

To run your application through the Streetlamp wrapper all you need to do is instantiate the Router class and call route. The Router will use a RouteBuilder to scan all of your namespaces in the composer.json (excluding test namespaces by default) and setup corresponding routes.

Here's all the code you need to get going:

<?php declare(strict_types=1);

require_once 'vendor/autoload.php';

use willitscale\Streetlamp\Router;

(new Router())->route();

This will use a simple out of the box configuration, if you require any customisation this can be achieved with the RouterConfig. There's a comprehensive guide on configuration in the Configuration page.

3.3 Creating a Controller

A controller can be defined by simply giving a class the attribute of RouteController.

<?php declare(strict_types=1);

namespace Example;

use willitscale\Streetlamp\Attributes\Controller\RouteController;

#[RouteController]
class MyRouteClass {
}

Only classes with the RouteController attribute will be scanned for routes.

3.4. Creating a Route

Each public method within a RouteController can be annotated as a route. There's three requirements to transform a method into a route:

  • add a HTTP method attribute to the method,
  • a path attribute to the method or class and
  • return the ResponseBuilder object.

Let's say we want to create a route for the request GET /hello HTTP/1.1, we would need to attribute our route method with the Get and Path attributes.

Here's what that would look like in code:

<?php declare(strict_types=1);

namespace Example;

use willitscale\Streetlamp\Attributes\Controller\RouteController;
use willitscale\Streetlamp\Attributes\Path;
use willitscale\Streetlamp\Attributes\Route\Method;
use willitscale\Streetlamp\Builders\ResponseBuilder;
use willitscale\Streetlamp\Enums\HttpMethod;
use willitscale\Streetlamp\Enums\HttpStatusCode;

#[RouteController]
class MyRouteClass
{
    #[Path('/hello')]
    #[Method(HttpMethod::GET)]
    public function simpleGet(): ResponseBuilder
    {
        return (new ResponseBuilder())
            ->setData('world')
            ->setHttpStatusCode(HttpStatusCode::HTTP_OK);
    }
}

We could have also applied the #[Path('/hello')] to the RouteController and then all routes defined within the controller have that path prefixed to them so you would not need to apply a path to them individually.

4. Futher Reading

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.