PHP code example of codeburner / router

1. Go to this page and download the library: Download codeburner/router 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/ */

    

codeburner / router example snippets


"GET" "/article/17"

$collector->get("/article/{id}", "ArticleResource::show");



use Codeburner\Router\Collector;
use Codeburner\Router\Matcher;

$collector = new Collector();
$matcher   = new Matcher($collector);

$collector->get("/", function () {
    echo "Hello World!";
});

$route = $matcher->match("get", "/");
$route->call();

"/articles/{article:[a-z0-9-]+}"

"/user/photos[/{id:uid}]"

"/{resource:string+}/get/{count:int+}" "{resource}Controller::getLimited"

$collector->get("/article/{id:int{5}}", function (RequestInterface $request, ResponseInterface $response, array $args) {
    return (string) getCommentById($args["id"]);
})->setStrategy(new RequestResponseStrategy($request, $response));

$collector->get("/article/{id:int{5}}", function (RequestInterface $request, array $args) {
    return (array) getCommentById($args["id"]);
})->setStrategy(new RequestJsonStrategy($request, $response));

$route = $collector->get("/{id:int{5}}", function (RequestInterface $request, ResponseInterface $response, array $args) {
    return $args["id"];
});

$route->call(function ($class) use ($container) {
    return $container->get($class);
});

$collector->get("/", function ($arg) {
    echo $arg;
})->setDefault("arg", "hello world");

$route->call(function ($class) use ($container) {
    return $container->get($class);
});

// ...
// The Path class will create several links for us, just give they new object a instance of the collector.
$path = new Codeburner\Router\Path($collector);
// ...
// Setting the name of route to blog.article
$collector->get("/blog/{article:slug+}", "blog::show")->setName("blog.article");
// ...
// this will print an anchor tag with "/blog/my-first-article" in href.
echo "<a href='", $path->to("blog.article", ["article" => "my-first-article"]), "'>My First Article</a>";

$collector->resource('PhotosResource');

// create only the index and show routes.
$collector->resource("ArticleResource", ["only" => ["index", "show"]]);

// create only the index and show routes too, because all the others should not be created.
$collector->resource("ArticleResource", ["except" => ["make", "create", "destroy", "update", "edit"]]);

// create only the index and show routes.
$collector->resource("ArticleResource")->only(["index", "show"]);

// create only the index and show routes too, because all the others should not be created.
$collector->resource("ArticleResource")->except["make", "create", "destroy", "update", "edit"]);

// now the pattern for make action will be /account/make
$collector->resource("UserResource", ["as" => "account"]);

$collector->resource("CategoryResource")->nest(
    $collector->resource("ArticleResource")
);

$collector->resource("CategoryResource")->nest(
    $collector->resource("ArticleResource")->nest(
        $collector->resource("CommentResource")
    )
);

"/category/1/article/2/comment/3"

$collector->resource("ArticleResource")->nest(
    $collector->resource("CommentResource")->only(["index", "make", "create"]);
);

$collector->resource("CommentResource")->except(["index", "make", "create"]);

$collector->resource("ArticleResource")->shallow(
    $collector->resource("CommentResource")
);

$collector->resource("PhotosResource")->member(
    $collector->get("/preview", "PhotosResource::preview")
);

class PhotosResource {
    public function index() {
    
    }
}

$collector->resource("PhotosResource")->only("index");
$collector->resource("PhotosResource", ["as" => "picture"])->only("index");

echo $path->to("photos.index"), "<br>", $path->to("picture.index");

$collector->resource("ArticleResource", ["as" => "kategorien", "translate" => ["new" => "neu", "edit": "bearbeiten"]);

$collector->resource("ArticleResource", ["as" => "kategorien"])->translate(["new" => "neu", "edit": "bearbeiten"]);

class UserController
{
    public function getName()
    {
        // the same as $collector->get("/user/name", "UserController::getName")
    }
}

class BlogController
{
    /**
     * @param int $id
     * @annotation MyActionExcecutorStrategy
     */
    public function getPost($id)
    {
        // the same as $collector->get("/blog/post/{id:int+}", "BlogController::getPost")
        //                       ->setStrategy("MyActionExecutorStrategy")
    }
}

$collector->setControllerActionJoin("-");
$collector->controller("UserController");

$collector->get("/foo", "controller::action");

try {
    $matcher->match("post", "/foo");
} catch (Codeburner\Router\Exceptions\MethodNotAllowedException $e) {
    // You can for example, redirect to the correct request.
    // this if verify if the requested route can serve get requests.
    if ($e->can("get")) {
        // if so, dispatch into get method.
        $matcher->match("get", $e->requested_uri);
    }
}