<?php // Objectiveweb Router // run() example // library dependencies // in real applications you should use an autoloader include '../vendor/level-2/dice/Dice.php'; include '../src/Router.php'; // application dependencies include 'App/HomeController.php'; include 'App/ProductsController.php'; include 'App/DB/ProductsRepository.php'; use Objectiveweb\Router; $app = new Router(); // app configuration $app->addRule('App\\DB\\ProductsRepository', ['shared' => true, 'constructParams' => [array(array('name' => "Cassete Recorder", 'sku' => 1, 'price' => 100.0), array('name' => "Tractor Beam", 'sku' => 2, 'price' => 7.99))]]); // Starts the application on the App namespace // Requests to /products will be mapped to App\ProductsController // Root and other requests are mapped to App\HomeController $app->run('App');
<?php // library dependencies // in real applications you should use an autoloader include '../vendor/level-2/dice/Dice.php'; include '../src/Router.php'; // application dependencies include 'App/ProductsController.php'; include 'App/DB/ProductsRepository.php'; use Objectiveweb\Router; $app = new Router(); // app configuration $app->addRule('App\\DB\\ProductsRepository', ['shared' => true, 'constructParams' => [array(array('name' => "Cassete Recorder", 'sku' => 1, 'price' => 100.0), array('name' => "Tractor Beam", 'sku' => 2, 'price' => 7.99))]]); $app->GET("/", function () { return <<<EOF <html> <body> <h1>Router Example page</h1> <h2>ProductsController</h2> <ul> <li><a href="index.php/products">Index: Products listing (json)</a></li> <li><a href="index.php/products/1">Path variable: Product detail</a></li> <li><a href="index.php/products/sale">Custom method: Products with 10% discount</a></li> <li><a href="index.php/products/sale/50">Custom method with path parameters: Products with 50% discount</a></li> <li><a href="index.php/products/anything/really?error=1">before(): Check error trigger</a></li> EOF; }); /** * Router::controller will bind a path to a class, using the following schema *
public static function redirect($to, $code = 307) { header("HTTP/1.1 {$code}"); header('Location: ' . Router::url($to)); exit; }