TheTrooper123 Posted June 13, 2022 Share Posted June 13, 2022 Hi all, I have built a basic router for my slug urls to work. However I'm trying to make it so that if a page is created (maybe for a blog) that the router would automatically work without have to hard code it in. This is my current code where I've hard coded the link 'post-one' to work: $request = $_SERVER['REQUEST_URI']; switch ($request) { case '/blog/post-one': $_GET['page_slug'] = 'post-one'; require __DIR__ . '/views/blog/page.php'; break; The below code won't work but hopefully gives an idea what I am trying to acheive where if a new post is created the 'page_slug' is retrieved and the dynamic link is created. Anyone know how something like this could work? $request = $_SERVER['REQUEST_URI']; switch ($request) { case '/blog/'.$_GET['page_slug'].'': $_GET['page_slug'] = $rows["page_slug"]; require __DIR__ . '/views/blog/page.php'; break; Any help most appriciated. Quote Link to comment https://forums.phpfreaks.com/topic/314919-dynamic-php-router/ Share on other sites More sharing options...
requinix Posted June 13, 2022 Share Posted June 13, 2022 Simple: don't do the routing in your PHP code. Set up your web server to automatically rewrite /blog/* to /views/blog/page.php?page_slug=*. The term you'll want to research is "URL rewriting"... Quote Link to comment https://forums.phpfreaks.com/topic/314919-dynamic-php-router/#findComment-1597233 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.