corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-16 339a2b74c2dcdd4ae57161daba48d39a64337fbc
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
65
66
67
68
<?php
 
declare(strict_types= 1);
 
set_error_handler(function(
    int $errno,
    string $errstr,
    string $errfile,
    int $errline
    ): bool 
    {
        throw new ErrorException($errstr,0, $errno, $errfile, $errline);
});
 
 
set_exception_handler(function (Throwable $exception) {
    static $show_errors = true;
 
    if($exception instanceof Framework\Exceptions\PageNotFoundException) {
        http_response_code(404);
    } else {
        http_response_code(500);
    }
 
    if($show_errors) {
        ini_set("display_errors", "1");
    }else{
        ini_set("display_errors","0");
        ini_set("log_errors","1");
        require "views/500.php";
    }
 
    throw $exception;
});
 
spl_autoload_register(function ($class) {
    require "src/". str_replace("\\", "/", $class). ".php";
 });
 
$path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
if ($path === false) {
    throw new UnexpectedValueException("Malformed URL: {$_SERVER["REQUEST_URI"]}");
}
 
$segments = explode("/", $path);
 
$router = new Framework\Router;
 
$router->add("/admin/{controller}/{action}", ["namespace" => "Admin"]);
$router->add("/product/{slug:[\w-]+}", ["controller" => "products", "action" => "show"]);
$router->add("/{title}/{id:\d+}/{page:\d+}", ["controller" => "products", "action" => "showPage"]);
$router->add("/{controller}/{id:\d+}/{action}");
$router->add("/home/index", ["controller" => "home", "action" => "index"]);
$router->add("/products", ["controller" => "products", "action" => "index"]);
$router->add("/", ["controller" => "home", "action" => "index"]);
$router->add("/{controller}/{action}");
 
$container = new Framework\Container;
 
 
$container->set(App\Database::class, function() {
    return new App\Database("localhost", "product_db", "product_db_user", "secret");
});
 
$dispatcher = new Framework\Dispatcher($router, $container);
 
$dispatcher->handle($path);