PHP code example of kento-oka / roust

1. Go to this page and download the library: Download kento-oka/roust library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kento-oka / roust example snippets


use Roust\Router;

$router = new Router();

use Roust\Router;
use Request;    //  Implemented Psr\Http\Message\ServerRequestInterface

$router     = new Router();
$request    = new Request();

//  It matches GET http://example.com/
$router->addRoute("GET", "/", [
    "controller"    => "index",
    "action"        => "index"
]);

//  It matches GET http://example.com/users/
$router->addRoute("GET", "/users/", [
    "controller"    => "user",
    "action"        => "index"
]);

//  It matches GET http://example.com/users/my/ and POST http://example.com/users/my/
$router->addRoute(["GET", "POST"], "users/my/", [
    "controller"    => "user",
    "action"        => "mypage"
]);

//  It matches GET http://example.com/users/123/
$router->addRoute("GET", "/users/{uid:[1-9][0-9]*}/", [
    "controller"    => "user",
    "action"        => "page"
]);

$result = $router->search($request->getMethod(), $request->getUri()->getPath());

switch($result["result"]){
    case Router::NOT_FOUND:
        // ... 404 Not Found
        break;
    case Router:METHOD_NOT_ALLOWED:
        $allowedMethods = $result["allowed"];
        // ... 405 Method Not Allowed
        break;
    case Router::FOUND:
        $params = $resul["params"];
        //  Do something
        break;
}

//  It matches GET http://example.com/
$router->addRoute("GET", "/", [
    "controller"    => "index",
    "action"        => "index"
]);

//  It matches GET http://example.com/users/
$router->addRoute("GET", "/users/", [
    "controller"    => "user",
    "action"        => "index"
]);

//  It matches GET http://example.com/users/my/ and POST http://example.com/users/my/
$router->addRoute(["GET", "POST"], "users/my/", [
    "controller"    => "user",
    "action"        => "mypage"
]);

$router->get("/users/", [
    "controller"    => "user",
    "action"        => "index"
]);

$router->addRoute("GET", "/users/{uid:[1-9][0-9]*}/", [
    "controller"    => "user",
    "action"        => "page"
]);

$router->addShortRegex("d", new \Roust\Sregex\NaturalNumber());

$router->addroute("GET", "/users/{uid:|d}/", [
    "controller"    => "user",
    "action"        => "page"
]);

$router->makePathGroup("/users", function($r){
    $r->get("/", [
        "controller"    => "user",
        "action"        => "index"
    ]);

    $r->makeParamsGroup(["Controller" => "user"], function($r){
        $r->get("/my/", [
            "action"    => "mypage"
        ]);

        $r->get("/{uid:[1-9][0-9]*}/", [
            "action"    => "page"
        ]);
    });
});

$route->addShortRegex("d", new NaturalNumber());
$router->get("/users/{id:[1-9][0-9]*}/", []);
$router->get("/users/{id:|d}/profile/", []);

$router->search("GET", "/users/123/");          // Not Found
$router->search("GET", "/users/123/profile");   // Found