corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-16 a2e337f27202d3e560e7a1672769ef0ed9d51def
commit | author | age
c4c562 1 <?php
7301d1 2
9e69c2 3 declare(strict_types= 1);
ef05b9 4 $show_errors = false;
addab2 5
F 6 if($show_errors) {
7     ini_set("display_errors", "1");
8 }else{
9     ini_set("display_errors","0");
a2e337 10     ini_set("log_errors","1");
ef05b9 11     require "views/500.php";
addab2 12 }
F 13
9e69c2 14
b62315 15 spl_autoload_register(function ($class) {
7301d1 16     require "src/". str_replace("\\", "/", $class). ".php";
b62315 17  });
0c028b 18
001175 19 $path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
9e69c2 20 if ($path === false) {
F 21     throw new UnexpectedValueException("Malformed URL: {$_SERVER["REQUEST_URI"]}");
22 }
23
001175 24 $segments = explode("/", $path);
F 25
7301d1 26 $router = new Framework\Router;
2d9ddb 27
0fc0f1 28 $router->add("/admin/{controller}/{action}", ["namespace" => "Admin"]);
2272ef 29 $router->add("/product/{slug:[\w-]+}", ["controller" => "products", "action" => "show"]);
1f822b 30 $router->add("/{title}/{id:\d+}/{page:\d+}", ["controller" => "products", "action" => "showPage"]);
c0c5ce 31 $router->add("/{controller}/{id:\d+}/{action}");
7adeb4 32 $router->add("/home/index", ["controller" => "home", "action" => "index"]);
F 33 $router->add("/products", ["controller" => "products", "action" => "index"]);
34 $router->add("/", ["controller" => "home", "action" => "index"]);
e53a93 35 $router->add("/{controller}/{action}");
4ce2a5 36
2bddb6 37 $container = new Framework\Container;
F 38
75438d 39
3880d4 40 $container->set(App\Database::class, function() {
75438d 41     return new App\Database("localhost", "product_db", "product_db_user", "secret");
3880d4 42 });
95ec24 43
2bddb6 44 $dispatcher = new Framework\Dispatcher($router, $container);
f6df29 45
F 46 $dispatcher->handle($path);
7bc6e3 47