php - How do you put dynamic pages after the url?

Solution:

You can create view called article, then in URL add a param, for example my-awesome-blog/article?articleId=1. In PHP code use $_REQUEST['articleID'] to get an ID from URL. If you get ID, execute MySQL query with WHERE `id` = $_REQUEST['articleID']. At the end display getted data in view.

Done!

Answer

Solution:

You have to make the page dynamic and store the data in a db. Also for rewriting url, you need to make use of the .htaccess directives. mod_rewrite is the function you will use to rewrite the url

Answer

Solution:

I guess you are looking for a router.

    class Router
    {
        public array $routes = [];

    public function Route($action, Closure $callback): void
    {
        $action = trim($action, '/');
        $action = preg_replace('/{[^}]+}/', '(.*)', $action);

        $this->routes[$action] = $callback;
    }

    public function Dispatch($action): void
    {
        $action = trim($action, '/');
        $callback = null;
        $params = [];

        foreach ($this->routes as $route => $handler) {
            if (preg_match("%^{$route}$%", $action, $matches) === 1) {
                $callback = $handler;
                unset($matches[0]);
                $params = $matches;
                break;
            }
        }

        if (!$callback || !is_callable($callback)) {
            http_response_code(404);
            exit;
        }

        echo $callback($params);
    }
}

This is a router class I made myself and it is very useful for small projects.

You add routes using another "config" router.php which will need to look like:

    $route = new Router();
    
    $route->Route('/', function() {
echo "<h1>Testing route</h1>";
    });
    $route->Route('/contact', function() {
    echo "<h1>Testing route Contact</h1>";
    });
    
    
    $action = $_SERVER['REQUEST_URI'];
    
    $route->Dispatch($action);

As .htaccess you will put:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]

Source