Giter Site home page Giter Site logo

splash-router's Introduction

Latest Stable Version Total Downloads Latest Unstable Version License Scrutinizer Code Quality Build Status Coverage Status

Splash: a highly configurable PSR-7 router

What is Splash?

Splash is a PHP router. It takes an HTTP request and dispatches it to the appropriate controller.

Clean controllers

Want to get a feeling of Splash? Here is a typical controller:

use TheCodingMachine\Splash\Annotations\Get;
use TheCodingMachine\Splash\Annotations\URL;

class MyController
{
    /**
     * @URL("/my/path")
     * @Get
     */
    public function index(ServerRequestInterface $request)
    {
        return new JsonResponse(["Hello" => "World!"]);
    }
}

Ok, so far, things should be fairly obvious to anyone used to PSR-7. The important parts are:

  • Routing is done using the @URL annotation. When a method has the @URL annotation, we call it an action.
  • Controllers are clean. They don't extend any "Splash" object (so are reusable in any other PSR-7 compatible MVC framework)
  • Actions can optionally have a @Get, @Post, @Put, @Delete annotation to restrict the response to some HTTP method.
  • Splash analyzes the action signature. If it finds a type-hinted ServerRequestInterface parameter, it will fill it the PSR-7 request object.
  • Actions must return an object implementing the PSR-7 ResponseInterface.

Even better

But Splash can provide much more than this.

Here is a more advanced routing scenario:

use TheCodingMachine\Splash\Annotations\Post;
use TheCodingMachine\Splash\Annotations\URL;
use Psr\Http\Message\UploadedFileInterface;

class MyController
{
    /**
     * @URL("/user/{id}")
     * @Post
     */
    public function index($id, $firstName, $lastName, UploadedFileInterface $logo)
    {
        return //...;
    }
}

Look at the signature: public function index($id, $firstName, $lastName, UploadedFileInterface $logo).

  • $id will be fetched from the URL (@URL("/user/{id}"))
  • $firstName and $lastName will be automatically fetched from the GET/POST parameters
  • finally, $logo will contain the uploaded file from $_FILES['logo']

See the magic? Just by looking at your method signature, you know what parameters your route is expecting. The method signature is self-documenting the route!

Even better, Splash is highly extensible. You can add your own plugins to automatically fill some parameters of the request (we call those ParameterFetchers). You could for instance write a parameter fetcher to automatically load Doctrine entities:

    /**
     * Lo and behold: you can extend Splash to automatically fill the function parameters with objects of your liking
     *
     * @URL("/product/{product}")
     * @Post
     */
    public function index(My\Entities\Product $product)
    {
        return //...;
    }

Best practices

You might wonder: "How will Splash instantiate your controller?" Well, Splash will not instantiate your controller. Instantiating services and containers is the role of the dependency injection container. Splash connects to any container-interop compatible container and will fetch your controllers from the container.

This means that you must declare your controller in the container you are using. This is actually a good thing as this encourages you to not use the container as a service locator.

High performance

For best performance, Splash is caching the list of routes it detects. Unlike what can be seen in most micro-frameworks where the application slows down as the number of routes increases, in Splash, performance stays constant as the number of routes increases.

splash-router's People

Contributors

camklcbr avatar charlesc-tcm avatar hugoaverty avatar kharhamel avatar moufmouf avatar ngob avatar nguyenk avatar npeguin avatar vaidiep avatar xhuberty avatar

Watchers

 avatar  avatar  avatar

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.