File size: 2,005 Bytes
7731906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
namespace App\Core;

class Router {
    protected $routes = [];

    public function __construct($routes) {
        $this->routes = $routes;
    }
 
    public function dispatch() {
        $uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
        
        // Dynamically detect base path (e.g., /buddhaword)
        $scriptPath = dirname($_SERVER['SCRIPT_NAME']);
        if ($scriptPath === '/') $scriptPath = '';
        
        if ($scriptPath && strpos($uri, $scriptPath) === 0) {
            $uri = substr($uri, strlen($scriptPath));
        }
        
        $uri = ($uri === '' || $uri === false) ? '/' : $uri;
        
        // Decode URI for matching against routes that might contain Lao/UTF-8 characters
        $decodedUri = urldecode($uri);

        foreach ($this->routes as $route => $action) {
            if ($this->match($route, $decodedUri, $params)) {
                $this->callAction($action, $params);
                return;
            }
        }

        // 404 Not Found
        http_response_code(404);
        echo "404 Not Found: " . htmlspecialchars($decodedUri) . " (Base: " . htmlspecialchars($scriptPath) . ")";
    }

    protected function match($route, $uri, &$params) {
        // Use the 'u' modifier for unicode support
        $pattern = preg_replace('/\{([a-zA-Z0-9_]+)\}/', '(?P<$1>[^/]+)', $route);
        $pattern = "#^" . $pattern . "$#u";

        if (preg_match($pattern, $uri, $matches)) {
            $params = array_filter($matches, 'is_string', ARRAY_FILTER_USE_KEY);
            return true;
        }

        return false;
    }

    protected function callAction($action, $params) {
        if (is_callable($action)) {
            call_user_func_array($action, $params);
            return;
        }

        list($controller, $method) = explode('@', $action);
        $controller = "App\\Controllers\\" . $controller;
        $instance = new $controller();
        call_user_func_array([$instance, $method], $params);
    }
}