corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-27 61ad98a6e2914578cae32f249d2b496892c0f10b
aggiunto controllo sul metodo post in modo da non poter raggiungere la route destroy utilizzando il get (parte 144)
4 files modified
18 ■■■■■ changed files
config/routes.php 2 ●●● patch | view | raw | blame | history
public/index.php 2 ●●● patch | view | raw | blame | history
src/Framework/Dispatcher.php 6 ●●●● patch | view | raw | blame | history
src/Framework/router.php 8 ●●●● patch | view | raw | blame | history
config/routes.php
@@ -11,7 +11,7 @@
$router->add("/{controller}/{id:\d}/edit", ["action"=> "edit"]);
$router->add("/{controller}/{id:\d}/update", ["action"=> "update"]);
$router->add("/{controller}/{id:\d}/delete", ["action"=> "delete"]);
$router->add("/{controller}/{id:\d}/destroy", ["action"=> "destroy"]);
$router->add("/{controller}/{id:\d}/destroy", ["action"=> "destroy", "method" => "post"]);
$router->add("/home/index", ["controller" => "home", "action" => "index"]);
public/index.php
@@ -28,5 +28,5 @@
$dispatcher = new Framework\Dispatcher($router, $container);
$dispatcher->handle($path);
$dispatcher->handle($path, $_SERVER["REQUEST_METHOD"]);
    
src/Framework/Dispatcher.php
@@ -9,11 +9,11 @@
    public function __construct(private Router $router,
                                private Container $container) { }
    public function handle(string $path) {
        $params = $this->router->match($path);
    public function handle(string $path, $method) {
        $params = $this->router->match($path, $method);
        if($params === false) {
            throw new PageNotFoundException("No route matched for '$path'");
            throw new PageNotFoundException("No route matched for '$path' with method '$method'");
        }
        $controller = "App\Controllers\\" . ucwords($params["controller"]);
src/Framework/router.php
@@ -12,7 +12,7 @@
    }
    public function match (string $path): array|bool {
    public function match (string $path, string $method): array|bool {
        $path = urldecode($path);
        
        $path = trim($path, "/");   
@@ -26,6 +26,12 @@
                $params = array_merge($matches, $route["params"]);
                if(array_key_exists("method", $params)) {
                    if(strtolower($method) !== strtolower($params["method"])) {
                        continue;
                    }
                }
                return $params;
            }
        }