为 CMS 动态生成页面链接
我进行了广泛的搜索,每个 CMS 教程要么根本没有解释这一点,要么为您提供了大量代码而不解释其工作原理。即使在堆栈溢出时,我也找不到任何接近答案的东西,尽管如果有人能指出我的答案,我也可以食言。
我在这个项目中使用 PHP 和 mysql。
我正在构建一个 CMS。它非常简单,我理解我认为我需要的每个概念,除了如何动态生成页面和页面链接。我想要做到这一点的方法是通过一个数据库表来存储页面的名称和页面的主要内容。就这样。然后我只需调用一个脚本将页面的主要内容提取到我碰巧调用的任何页面中。没什么大不了的,对吧?错误的。
问题就在这里。如果我要这样做,那么我必须为我想要创建的每个页面创建一个文件,该文件调用从正确的数据库行中提取内容的脚本。因此,我可以将各种页面名称和内容添加到表中,但我不知道如何调用它们,而无需每次想要链接到新页面时手动创建新文件。
理想情况下,有一个脚本可以在创建页面时根据数据库表的页面名称行创建页面链接。但是如何获得那些末尾带有 ?=pageName
的链接呢?如果我知道它是如何工作的,那么我就能弄清楚剩下的事情。
更新 第二个答案确实证实了我认为我必须做的一切,但有一个问题。我现在的计划是将所有代码分成一系列函数,并将它们包含或要求在不同的模板中,这些模板将用于格式化页面的显示方式。我需要一种主页外观和一种其他页面设计。我想我会有一个函数,如果 ID 为 0,则调用此页面 template.php,否则调用此另一个模板 file.php。但是如何将所需的变量传递给这些新文件呢?我是否只在其中包含index.PHP 页面?
I've searched far and wide and every CMS tutorial out there either doesn't explain this at all or gives you a huge chunk of code without explaining how it works. Even on stack overflow I can't find anything close to the answer, though I'd be okay with eating my words if someone could point me to the answer.
I am using PHP and mysql for this project.
I am building a CMS. Its extremely simple and I understand every concept I think I'll need except how to dynamically generate pages and page links. The way I want to do it is by having a database table that stores the name of a page and the main content of the page. That's all. Then I'd just call a script to pull the main content of a page into whatever page I happen to call. No big deal, right? Wrong.
Here's the problem. If I were to do this then I'd have to create a file for every page I want to create that calls the script that pulls the content from the correct database row. So I could add all sorts of page names and contents into the table but I don't know how to call them without manually creating new files each time I want to link to a new page.
Ideally there'd be a script that creates links to pages based on the page name row of the DB table as the pages are created. But how do you get those links with the ?=pageName
at the end? If I just knew how that worked then I could figure the rest out.
UPDATE
The second answer really confirmed everything I thought I had to do but there is one catch. My plan now is to split up all the code into a series of functions and either include or require them in different templates that will be used to format the way pages are displayed. I need one look for the home page and one other design for the rest of the pages. I'm thinking that I'll have a function that says if ID is 0 then call this page template.php else call this other template file.php. But how do I pass the required variables to these new files? Do I just include the index.PHP page in them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
账单你实际上走在正确的轨道上。如今几乎所有网络软件都会进行大量的 URL 处理。传统上,您的 Web 根目录上会有 php 页面,然后利用 URL 中的查询字符串来优化页面的输出。您已经明白为什么这可能是不需要的。因此,流行的替代方案是前端控制器设计模式。基本上,我们将每个请求集中到您的 index.php 页面,然后将请求路由到 Web 根目录之外的内部页面或应用程序。这可能会很快变得复杂,而且每个人似乎都以独特的方式实现这种模式。
我们可以通过简单地将我们的应用程序放在索引页面中来利用这种模式,而无需路由。下面的脚本显示了您尝试以最简单的方式执行操作的示例。我们的脚本基本上只有一页。我们可以通过更改 url 中的 id 查询字符串来请求虚拟页面。例如,www.demo.net/?id=0 可以用作您网站的索引。这应该与没有“id”查询的 www.demo.net 相同。即使你不知道问题是什么,也要继续一一解决这些问题。一旦您开始查看其他人的代码,您就可以开始了解其他人如何解决您遇到的相同问题。
下面的解决方案将帮助您入门,但是当您需要管理页面时该怎么办?您如何验证用户身份?您是否为另一个页面复制了大量代码?如果您认真对待您的 CMS,那么您将希望在其之下实现某种框架。一个用于处理 url、路由到应用程序、加载配置文件以及可能管理数据库连接的框架。是的,它会变得很复杂,但如果你一次解决每个问题就不会那么复杂。利用类或函数来共享代码来启动。至少在页面顶部包含一个通用的“引导”文件来初始化数据库连接等通用功能。阅读 Stack Overflow 只是为了了解最新动态。您可以学习很多术语,并且可能找到一些您甚至不知道自己想问的问题的答案。
下面假设我们有一个包含以下字段的表:
Bill your actually on the right track. Almost all web software today does extensive URL processing. Traditionally you would have php pages on your web root and then utilize the query string in the URL to refine the page's output. You have already arrived at why this might not be desired. So the popular alternative is the Front Controller design pattern. Basically we funnel every request to your index.php page and then route the request to internal pages or apps outside the web root. This can get complicated fast and everybody seems to implement this pattern in unique ways.
We can utilize this pattern without the routing by simply putting our app in the index page. The script below shows an example of what your trying to do in the simplest of ways. We basically have one page with our script. We can request the virtual pages by changing the id query string in our url. For example www.demo.net/?id=0 can be utilized as an index to your site. This should be the same as www.demo.net without the 'id' query. Just keep solving those problems one by one even if you don't know what the problem is. Once you start looking at other peoples code, then you can start seeing how other people solved the same problems you have.
The solution below will get you started, but then what do you do when you want an admin page? How do you authenticate the user? Do you duplicate alot of the code for yet another page? If your serious about your CMS then your going to want to implement some kind of framework underneath it. A framework to process the url, route to your application, load configuration files, and probably manage your database connection. Yea it gets complicated, but not if you solve each problem one at a time. Utilize classes or functions to share code to start. At the very least include a common "bootstrap" file at the top of your page to initialize common functionality such as a database connection. Read Stack Overflow just to keep up with whats going on. You can learn alot of terminology and probably find some answers to questions you didn't even know you wanted to ask.
Below assume we have a table with the following fields:
我认为您需要阅读有关
$_GET
的内容。我还推荐一本不错的 PHP 书籍。忘记在线教程;它们(在大多数情况下)完全无用。
I think you need to read about
$_GET
.I also recommend a decent PHP book. Forget online tutorials; they are (for the most part) utterly useless.