yarf/yarf

Yet another routing framework

0.0.9 2016-12-18 22:06 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:11:42 UTC


README

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);