博客文章的PHP路由系统

发布于 2025-02-06 19:02:19 字数 595 浏览 0 评论 0原文

我设法创建了一个简单的路由系统,但是我对如何使其更具动态性有些困扰。在这种情况下,我的路由中有一个博客文章,该文章很难编码。如果我在网站上单击一个需要/views/blog/page.php的动态链接,我该如何使系统能够算出page_slug是什么并附加page_slug,以便它可以在不用硬编码链接中进行硬编码的链接工作?

switch ($request) {

    case '':
    case '/':
        require __DIR__ . '/views/index.php';
        break;
    
    case '/blog/blog-post-one':
         $_GET['page_slug'] = 'blog-post-one';
        require __DIR__ . '/views/blog/page.php';
        break;
}

我在下面添加了.htaccess文件,以防万一

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

I have managed to create a simple routing system however I'm a bit stuck on how to make it more dynamic. In this case I have got a blog post set up in my routing which is hard coded. If i click a dynamic link in my website that requires the /views/blog/page.php how can I get the system to work out what the page_slug is and append the page_slug so that it works without hard coding the links into it?

switch ($request) {

    case '':
    case '/':
        require __DIR__ . '/views/index.php';
        break;
    
    case '/blog/blog-post-one':
         $_GET['page_slug'] = 'blog-post-one';
        require __DIR__ . '/views/blog/page.php';
        break;
}

I've added my .htaccess file below just in case

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文