public function testRequestMethods() { // Create Router $router = new \Bramus\Router\Router(); $router->get('/', function () { echo 'get'; }); $router->post('/', function () { echo 'post'; }); $router->put('/', function () { echo 'put'; }); $router->patch('/', function () { echo 'patch'; }); $router->delete('/', function () { echo 'delete'; }); $router->options('/', function () { echo 'options'; }); // Test GET ob_start(); $_SERVER['REQUEST_URI'] = '/'; $router->run(); $this->assertEquals('get', ob_get_contents()); // Test POST ob_clean(); $_SERVER['REQUEST_METHOD'] = 'POST'; $router->run(); $this->assertEquals('post', ob_get_contents()); // Test PUT ob_clean(); $_SERVER['REQUEST_METHOD'] = 'PUT'; $router->run(); $this->assertEquals('put', ob_get_contents()); // Test PATCH ob_clean(); $_SERVER['REQUEST_METHOD'] = 'PATCH'; $router->run(); $this->assertEquals('patch', ob_get_contents()); // Test DELETE ob_clean(); $_SERVER['REQUEST_METHOD'] = 'DELETE'; $router->run(); $this->assertEquals('delete', ob_get_contents()); // Test OPTIONS ob_clean(); $_SERVER['REQUEST_METHOD'] = 'OPTIONS'; $router->run(); $this->assertEquals('options', ob_get_contents()); // Test HEAD ob_clean(); $_SERVER['REQUEST_METHOD'] = 'HEAD'; $router->run(); $this->assertEquals('', ob_get_contents()); // Cleanup ob_end_clean(); }
} else { echo "index.js file not found."; } return $response; }); $router->post('/post/url', function () { $serviceName = filter_input(INPUT_POST, "sel-service"); $longUrl = filter_input(INPUT_POST, "longUrl"); if (isset($serviceName) && isset($longUrl)) { $key = file_get_contents("auth/key.txt"); $key = json_decode($key, true); if ($serviceName === "goo.gl") { $config = array('service-name' => $serviceName, 'longUrl' => $longUrl, 'apiKey' => $key[$serviceName]); } else { if ($serviceName === "bit.ly") { $config = array('service-name' => $serviceName, 'longUrl' => $longUrl, 'apiKey' => $key[$serviceName]["apiKey"], 'login' => $key[$serviceName]["login"]); } else { $config = array('service-name' => $serviceName, 'longUrl' => $longUrl); } } $bundle = new \peter\components\serviceBundle\serviceBundle($config); echo json_encode($bundle->sendReq()); } else { echo "Oops ! no data input"; } }); $router->set404(function () { header('HTTP/1.1 404 Not Found'); // ... do something special here echo file_get_contents("404.html"); });
echo 'Blog month overview (' . $year . '-' . $month . ')'; return; } if (!$slug) { echo 'Blog day overview (' . $year . '-' . $month . '-' . $day . ')'; return; } echo 'Blogpost ' . htmlentities($slug) . ' detail (' . $year . '-' . $month . '-' . $day . ')'; }); // Subrouting $router->mount('/movies', function () use($router) { // will result in '/movies' $router->get('/', function () { echo 'movies overview'; }); // will result in '/movies' $router->post('/', function () { echo 'add movie'; }); // will result in '/movies/id' $router->get('/(\\d+)', function ($id) { echo 'movie id ' . htmlentities($id); }); // will result in '/movies/id' $router->put('/(\\d+)', function ($id) { echo 'Update movie id ' . htmlentities($id); }); }); // Thunderbirds are go! $router->run(); // EOF