GithubHelp home page GithubHelp logo

yarf's Introduction

Yet another routing framework

Build Status Coverage Status Code Climate

Latest Stable Version Latest Unstable Version

A simple but powerful routing framework for PHP.

Basic usage

To create a new router responding to the root url of your server, simply do the following:

$router = new \Yarf\Router();

$classMap = [
  "" => IndexPage::class
];

$router->route($classMap);

Page types

There are three basic page types. HtmlPage, JsonPage and TextPage. Each of them is subclassable and meant to return either html, json or plain text. The content type for the http response is set appropriately.

Features

Rest support

Create a restful service with this framework is dead simple. Here is a quick example:

use Yarf\response\Response;

class UserApi extends Yarf\page\JsonPage {

  private $user;
  
  public function initialize() {
    $this->user = ...;
  }

  public function get(Response $response) {
    $response->result($this->user);
    return $response;
  }
  
  public function post(Response $response) {
    $this->user->update(...);
    $response->result($this->user);
    return $response;
  }
  
  public function delete(Response $response) {
    $response->result($this->user->delete());
    return $response;
  }

}

URI variables

When configuring your routes, route elements using a scheme like {variable} will be interpreted as variable uri parts.

use Yarf\response\Response;

class UserApi extents \Yarf\page\JsonPage {

  public function get(Response $response, $userId) {
    $response->result(UserCache::get($userId));
    return $response;
  }

}

$classMap = [
  "api" => [
    "user" => [
      "{userId}" => UserApi::class
    ]
  ]
]

$router = new Yarf\Router();
$router->route($classMap);

yarf's People

Contributors

jannisfink avatar

Watchers

 avatar

yarf's Issues

Pagemap chaining

To prevent the pagemap from becoming too large, it should be possible to chain pagemaps

Get rid of the php warnings

This is very annoying (though they are there for a reason). First ideas:

  • remove all the functions for responding to the requests form the superclass, so the programmer has to know which function they have to use. This would make sure that no warnings are logged, but the IDE support for auto completion does not work so well.
  • ignore the warnings (I don't like this)
  • suppress them (see above)

Possibility for fallthrough

It should be possible to fall through the routing function, if there is no handler. So the router can replace an existing one piece b piece.

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.