博客引擎如何创建博客文章?
我正在创建一个博客引擎作为学习练习,但一个特殊问题让我难住了。 我想知道博客文章是如何在 WordPress 等博客引擎中创建的? 我认为有两种方法可以做到这一点:
1) 创建名为“testPost”的新博客文章会创建一个名为 www.myblog.com/testPost.html 的新 HTML 页面。 因此,对于每一篇新博客文章,您都将一个新的 HTML 页面保存到服务器。 这个方法看起来效率很低。 一个博客可以有数百篇博客文章,这意味着您必须创建数百个 HTML 页面。 我想我不想使用这种方法。
2) 您有一个通用博客文章页面,其数据根据您尝试访问的文章呈现。 例如,如果我创建“testPostOne”,则通用博客文章页面将填充 testPostOne 的数据和 URL,如果我创建“testPostTwo”,则通用页面将呈现 testPostTwo 的相应内容,依此类推。
但使用这种方法也有其自身的问题。 例如,您将如何链接到实际不存在的页面? 链接到 http://www.myblog.com/testPostOne.html 将不起作用。
这是我可以想出的两种方法来解决这个问题。 我不确定是否还有其他选择。 如果您知道解决此问题的更好方法,请随时推荐。
基本上,我希望能够为每一篇博客文章提供一个格式良好的 URL,而不必在服务器上为每一篇文章创建一个新的 HTML 页面。
编辑:我可能会补充一点,我正在使用 ASP.NET 来执行此操作,因此通过此框架提供的任何方法都会有所帮助
I'm creating a blog engine as a learning exercise and one particular problem has me stumped. I'm wondering how are blog posts created in say a blog engine such as Wordpress? I'm thinking there are 2 ways you can do this:
1) Creating a new blog post called 'testPost' creates a new HTML page called www.myblog.com/testPost.html. So, for each new blog post you save a new HTML page to the server. This method seems inefficient. A blog can have hundreds of blog posts, which means you would have to create hundreds of HTML pages. I don't think I want to use this method.
2) You have a generic blog post page whose data is rendered according to the post you are trying to access. For example, if I created 'testPostOne' the generic blog post page would be filled with testPostOne's data and URL, if I created 'testPostTwo' then the generic page would render testPostTwo's respective contents and so on.
But using this method brings its own problems. For example how would you link to a page that doesn't actually exist? Linking to http://www.myblog.com/testPostOne.html would not work.
These are the two ways I could come up with to solve this problem. I'm not sure whether there are other options. Please feel free to recommend a better way of solving this problem if you know of one.
Basically, I want to be able to have a nicely formatted URL for each blog post without having to create a new HTML page on the server for each one.
EDIT: I might add that I'm using ASP.NET to do this so any methods available via this framework would be helpful
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
基本想法是使用数据库。 每个帖子都是数据库中的一个条目,您只需根据 URL 检索数据即可。 例如,
www.myblog.com/posts.php ?postid=1
或者
www.myblog.com/posts.aspx?postid=1
然后,您可以使用 URL 重写方法以更清晰的 URL 检索相同的帖子,或者更好的是使用 RESTful 方法来执行相同的任务。
The basic idea would be to use a database. Each posting would be an entry in the DB, and you simply retrieve the data depending on the URL. For example,
www.myblog.com/posts.php?postid=1
or
www.myblog.com/posts.aspx?postid=1
You can then either use URL rewrite methods to retrieve the same post with a cleaner URL, or better yet a RESTful method to do the same task.
这是用 ASP.NET 2.0 编写的一个开源博客引擎和一个用 PHP 编写(还有很多其他的)。 最好的办法是检查设计和架构,并剖析它(或类似的东西)是如何工作的。
Here is an open source blog engine written in ASP.NET 2.0 and one written in PHP (there are many others out there). You best bet is to check out the design and architecture and dissect how it (or something like it) does it job.
您需要使用 PHP 等语言创建一个动态页面,从数据库中读取帖子内容的数据。 如果您希望页面具有漂亮的 URL,那么您需要研究类似
mod_rewrite
的内容来重写 URL。You'll need to make a dynamic page in, say, PHP that reads data from a database for the post content. If you want pretty URLs with your page, then you'll want to look into something like
mod_rewrite
for rewriting the URLs.如果您动态创建页面(如建议 2 中所示),则 http://www.myblog.com/当您尝试访问 testPostOne.html 时,即使它不是磁盘上的实际文件,它也会存在......
所以建议 2 可能是最好的方法。
If you dynamically create the page (as in suggestion 2), the http://www.myblog.com/testPostOne.html will exist when you try to access it, even though it is not an actuall file on the disk...
So suggestion 2 is probably the best way to go.
就我个人而言,我使用 Apache mod_rewrite。 因此,当您拥有如下 URL:
http://myblog.com/archives/my_very_first_post
时,您可以制定如下重写规则:
Apache 将“my_very_first_post”解释为帖子 ID 并将其提供给处理 ID 的 PHP 脚本。 然后,该脚本从数据库中获取帖子并显示它。
我相信这是最常见的方法。
Personally, I use the Apache mod_rewrite. So when you have an URL like:
http://myblog.com/archives/my_very_first_post
,You can make a rewrite rule like this:
Apache interprets "my_very_first_post" as a post ID and feeds it to a PHP script that handles the ID. The script then Fetches the post from database and displays it.
I believe this is the most common approach.