# 路由

# Ngnix重写

location ~.*\.(sql|pem) {
  deny all;
}  
location / {
    if (!-e $request_filename){
        rewrite ^(.*)$ /index.php last;
    }  
}

# 请求

boot/router.php

# get|post|put|delete|options

$router->get('/about', function() {
    echo 'About Page Contents';
}); 

$router->get('/hello/(\w+)', function($name) {
    echo 'Hello ' . htmlentities($name);
});

$router->get('/movies/(\d+)/photos/(\d+)', function($movieId, $photoId) {
    echo 'Movie #' . $movieId . ', photo #' . $photoId;
});

$router->get(
    '/blog(/\d+)?(/\d+)?(/\d+)?(/[a-z0-9_-]+)?',
    function($year = null, $month = null, $day = null, $slug = null) {
        if (!$year) { echo 'Blog overview'; return; }
        if (!$month) { echo 'Blog year overview'; return; }
        if (!$day) { echo 'Blog month overview'; return; }
        if (!$slug) { echo 'Blog day overview'; return; }
        echo 'Blogpost ' . htmlentities($slug) . ' detail';
    }
);

$router->get('/blog(/\d{4}(/\d{2}(/\d{2}(/[a-z0-9_-]+)?)?)?)?', function($year = null, $month = null, $day = null, $slug = null) {
    // ...
});

正则说明

\d+  One or more digits (0-9)
\w+   One or more word characters (a-z 0-9 _)
[a-z0-9_-]+  One or more word characters (a-z 0-9 _) and the dash (-)
.*   Any character (including /), zero or more
[^/]+   Any character but /, one or more

# namespace

$router->setNamespace('\App\Controllers');
$router->get('/users/(\d+)', 'User@showProfile');
$router->get('/cars/(\d+)', 'Car@showProfile');

# 404

$router->set404('\App\Controllers\Error@notFound');

# Middlewares

$router->before('GET|POST', '/admin/.*', function() {
    if (!isset($_SESSION['user'])) {
        header('location: /auth/login');
        exit();
    }
});

$router->before('GET', '/.*', function() {
    // ... this will always be executed
});