1. Go to this page and download the library: Download gteixeira/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/ */
gteixeira / router example snippets
use CoffeeCode\Router\Router;
$router = new Router("https://www.youdomain.com");
/**
* routes
* The controller must be in the namespace Test\Controller
*/
$router->namespace("Test")->group("name");
$router->get("/", "Name:home", "name.home");
$router->get("/hello", "Name:hello", "name.hello");
$router->get("/redirect", "Name:redirect", "name.redirect");
/**
* This method executes the routes
*/
$router->dispatch();
/*
* Redirect all errors
*/
if ($router->error()) {
$router->redirect("name.hello");
}
class Name
{
public function __construct($router)
{
$this->router = $router;
}
public function home(): void
{
echo "<h1>Home</h1>";
echo "<p>", $this->router->route("name.home"), "</p>";
echo "<p>", $this->router->route("name.hello"), "</p>";
echo "<p>", $this->router->route("name.redirect"), "</p>";
}
public function redirect(): void
{
$this->router->redirect("name.hello");
}
}
use DI\ContainerBuilder;
use DI\Container;
use CoffeeCode\Router\Router;
$builder = new ContainerBuilder();
$builder->addDefinitions([
Router::class => function (Container $container) {
return new Router(projectUrl: "https://www.youdomain.com", container: $container);
}
]);
$container = $builder->build();
$router = $container->get(Router::class);
/**
* routes
* The controller must be in the namespace Test\Controller
*/
$router->namespace("Test")->group("name");
$router->get("/", "Name:home", "name.home");
$router->get("/hello", "Name:hello", "name.hello");
$router->get("/redirect", "Name:redirect", "name.redirect");
/**
* This method executes the routes
*/
$router->dispatch();
/*
* Redirect all errors
*/
if ($router->error()) {
$router->redirect("name.hello");
}
use CoffeeCode\Router\Router;
class Name
{
public function __construct(
protected Router $router
) { }
public function home(): void
{
echo "<h1>Home</h1>";
echo "<p>", $this->router->route("name.home"), "</p>";
echo "<p>", $this->router->route("name.hello"), "</p>";
echo "<p>", $this->router->route("name.redirect"), "</p>";
}
public function redirect(): void
{
$this->router->redirect("name.hello");
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.