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();
 }
示例#2
0
        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