Ejemplo n.º 1
0
$router = new \Bramus\Router\Router();
// Custom 404 Handler
$router->set404(function () {
    header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
    echo '404, route not found!';
});
// Before Router Middleware
$router->before('GET', '/.*', function () {
    header('Content-Type: application/json');
});
// // Static route: / (homepage)
// $router->get('/', function () {
// 	echo '';
// });
$router->mount('/post', function () use($router) {
    // Route: /posts (fetch all posts)
    $router->get('/', function () {
        $postModel = new Post();
        $posts = $postModel->getAllPosts();
        echo json_encode($posts);
    });
    // Route: /post/id (fetch a single post)
    $router->get('/(\\d+)', function ($id) {
        $postModel = new Post();
        $post = $postModel->getPost($id);
        echo json_encode($post);
    });
});
// Thunderbirds are go!
$router->run();
// EOF
Ejemplo n.º 2
0
 public function testSubrouteMouting()
 {
     // Create Router
     $router = new \Bramus\Router\Router();
     $router->mount('/movies', function () use($router) {
         $router->get('/', function () {
             echo 'overview';
         });
         $router->get('/(\\d+)', function ($id) {
             echo htmlentities($id);
         });
     });
     // Test the /movies route
     ob_start();
     $_SERVER['REQUEST_URI'] = '/movies';
     $router->run();
     $this->assertEquals('overview', ob_get_contents());
     // Test the /hello/bramus route
     ob_clean();
     $_SERVER['REQUEST_URI'] = '/movies/1';
     $router->run();
     $this->assertEquals('1', ob_get_contents());
     // Cleanup
     ob_end_clean();
 }
Ejemplo n.º 3
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