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/ */
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();
$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"]);
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->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);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.