reactphp-x/route

v1.0.0 2024-09-19 04:37 UTC

This package is auto-updated.

Last update: 2024-11-19 05:04:22 UTC


README

"Framework X has provided a wealth of inspiration." - Framework X

Like Laravel Route support

  • middleware
  • group

install

composer require reactphp-x/route -vvv

usage

in http server

use ReactphpX\Route\Route;
use FrameworkX\AccessLogHandler;
use FrameworkX\ErrorHandler;

$container = new DI\Container();

$route = new Route($container);

$route->get('/', function () {
    return new React\Http\Message\Response(
        200,
        ['Content-Type' => 'text/plain'],
        "Hello, World!\n"
    );
});
$route->group('/api', function ($route) {
    $route->get('/hello', function () {
        return new React\Http\Message\Response(
            200,
            ['Content-Type' => 'text/plain'],
            "Hello, API!\n"
        );
    });
    $route->get('/world', function () {
        return new React\Http\Message\Response(
            200,
            ['Content-Type' => 'text/plain'],
            "World, API!\n"
        );
    });
});

$class = new class {
    public function index() 
    {
        return new React\Http\Message\Response(
            200,
            ['Content-Type' => 'text/plain'],
            "Hello, Class!\n"
        );
    }
};

$route->get('/at', get_class($class).'@index');


$http = new React\Http\HttpServer(
    new AccessLogHandler(),
    new ErrorHandler(),
    $route
);

$socket = new React\Socket\SocketServer(getenv('X_LISTEN'));
$http->listen($socket);

echo "Server running at http://".getenv('X_LISTEN'). PHP_EOL;

in FrameworkX

use ReactphpX\Route\FrameworkXRoute;


$app = new FrameworkX\App();

$route = new FrameworkXRoute($app);

$route->get('/', function () {
    return new React\Http\Message\Response(
        200,
        ['Content-Type' => 'text/plain'],
        "Hello, World!\n"
    );
});
$route->group('/api', function ($route) {
    $route->get('/hello', function () {
        return new React\Http\Message\Response(
            200,
            ['Content-Type' => 'text/plain'],
            "Hello, API!\n"
        );
    });
    $route->get('/world', function () {
        return new React\Http\Message\Response(
            200,
            ['Content-Type' => 'text/plain'],
            "World, API!\n"
        );
    });
});

// 不支持 @ 
// $class = new class {
//     public function index() 
//     {
//         return new React\Http\Message\Response(
//             200,
//             ['Content-Type' => 'text/plain'],
//             "Hello, Class!\n"
//         );
//     }
// };

// $route->get('/at', get_class($class).'@index');

$app->run();

echo "Server running at http://".getenv('X_LISTEN'). PHP_EOL;

in FrameworkX and support @

use ReactphpX\Route\Route;

$container = new DI\Container();
$route = new Route($container);
$app = new FrameworkX\App(
    $route
);


//$app->any('{path:.*}', $route);

$route->get('/', function () {
    return new React\Http\Message\Response(
        200,
        ['Content-Type' => 'text/plain'],
        "Hello, World!\n"
    );
});
$route->group('/api', function ($route) {
    $route->get('/hello', function () {
        return new React\Http\Message\Response(
            200,
            ['Content-Type' => 'text/plain'],
            "Hello, API!\n"
        );
    });
    $route->get('/world', function () {
        return new React\Http\Message\Response(
            200,
            ['Content-Type' => 'text/plain'],
            "World, API!\n"
        );
    });
});

$class = new class {
    public function index() 
    {
        return new React\Http\Message\Response(
            200,
            ['Content-Type' => 'text/plain'],
            "Hello, Class!\n"
        );
    }
};

$route->get('/at', get_class($class).'@index');

$app->run();

echo "Server running at http://".getenv('X_LISTEN'). PHP_EOL;

Tests

./vendor/bin/phpunit tests/RoutesTest.php

License

MIT