Obullo / Router

Build Status Software License Total Downloads

Obullo router Django Url Dispatcher kütüphanesinden ilham alınarak geliştirilmiş bağımsız bir route paketidir.

Yükleme

$ composer require obullo/router

Host konfigürasyonu

Configuration.md

Gereksinimler

Bu versiyon aşağıdaki PHP sürümleri tarafından destekleniyor.

Test

$ vendor/bin/phpunit

Hızlı başlangıç

require '../vendor/autoload.php';

use Obullo\Router\Route;
use Obullo\Router\RequestContext;
use Obullo\Router\RouteCollection;
use Obullo\Router\Router;
use Obullo\Router\Types\{
    StrType,
    IntType
};
$config = array(
    'types' => [
        new IntType('<int:id>'),
        new StrType('<str:name>'),
    ]
);

Psr7 İsteği

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();
$context = new RequestContext;
$context->fromRequest($request);

Route Kolleksiyonu

$collection = new RouteCollection($config);
$collection->setContext($context);
$collection->add('home', new Route('GET', '/', 'App\Controller\DefaultController::index'));
$collection->add(
    'dummy',
    new Route(
        'GET',
        '/dummy/index/<int:id>/<str:name>',
        'App\Controller\DummyController::index'
        ['App\Middleware\Dummy::class']
    )
);

Route Sınıfı

$route = $collection->get('dummy');

echo $route->getHandler(); //  "App\Controller\DummyController::index"
echo $route->getMethods()[0]; // GET
echo $route->getPattern(); //  "/dummy/index/(?\d+)/(?\w+)"
echo $route->getStack()[0]; // App\Middleware\Dummy::class

Url Çözümleme

$router = new Router($collection);

if ($route = $router->matchRequest()) {

    $handler = $route->getHandler();
    $args = array_merge(array('request' => $request), $route->getArguments());
    $response = null;
    if (is_callable($handler)) {
        $exp = explode('::', $handler);
        $class = new $exp[0];
        $method = $exp[1];
        $response = call_user_func_array(array($class, $method), $args);
    }
    if ($response instanceof Psr\Http\Message\ResponseInterface) {
        echo $response->getBody();  // DummyController::index
    }
}

App\Controller\DummyController örneği

namespace App\Controller;

use Zend\Diactoros\Response\HtmlResponse;
use Psr\Http\Message\RequestInterface as Request;

class DummyController
{
    public function index(Request $request)
    {
        return new HtmlResponse('DummyController::index');
    }
}

Türler

Types.md

Kolleksiyon oluşturucu

Builder.md

Pipe ile gruplama

Pipes.md

Url çözümleme

Router.md

Koşullu tanımlamalar

RouteConditions.md

Katmanlar

Stack.md

Yerelleştirme

Translation.md

Url üretici

Generator.md

Performans

Performance.md