PHP code example of starbug / routing

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

    

starbug / routing example snippets


namespace MyApp;

use Starbug\Routing\RouteProviderInterface;
use Starbug\Routing\Controller;
use Starbug\Routing\Controller\ViewController;

class RouteProvider implements RouteProviderInterface {
  /**
   * @param Route $routes This is the root path "/"
   */
  public function configure(Route $routes) {
    // Configure the root path
    $routes->setController(ViewController::class);
    $routes->setOption("view", "home.html");

    // This Route is added from the root so the full path is "/" + "home" = "/home"
    $home = $routes->addRoute("home", ViewController::class, [
      "view" => "home.html"
    ]);
    $routes->addRoute("missing", [Controller::class, "missing"]);
    $routes->addRoute("forbidden", [Controller::class, "forbidden"]);

    // Adding from the above "/home" Route, the full path will  be "/home/test"
    $home->addRoute("/test", ViewController::class, [
      "view" => "test.html"
    ]);
  }
}

namespace MyApp;

use Starbug\Routing\Configuration;

$config = new Configuration();
$provider = new RouteProvider();

$config->addProvider($provider);

namespace MyApp;

use Starbug\Routing\FastRouteStorage;

$dispatcher = FastRouteStorage::createDispatcher($config);
$storage = new FastRouteStorage($dispatcher, $access);


namespace MyApp;

use Starbug\Routing\Router;

// Must be instance of Invoker\InvokerInterface
$invoker;

$router = new Router($invoker);
$router->addStorage($storage);

// Pass in a PSR-7 ServerRequestInterface instance and get back the route.
$route = $this->router->route($request);