简体   繁体   English

从数据库添加动态内容

[英]Adding dynamic content from a database

I'm using a new no-name framework that I'm not completely familiar with and I'm hung up on how I will add dynamic content from the database in this environment. 我正在使用一个我不完全熟悉的新的无名称框架,并且我挂念了如何在这种环境下从数据库添加动态内容。

In the index controller, I'm forced to have an index function that answers all requests for index.html. 在索引控制器中,我被迫拥有一个索引函数来回答对index.html的所有请求。 If I wanted to create a second page dynamically from a data source, how do I go about this without having to add a controller for this new page? 如果要从数据源动态创建第二个页面,该如何处理而不必为该新页面添加控制器? All requests are currently being routed by htaccess. htaccess当前正在路由所有请求。 Without getting into code snippets, I'm simply looking for the logic on how this is generally handled. 无需深入研究代码片段,我只是在寻找通常如何处理的逻辑。

Okay, the question is a bit vague, so the answer might seem a little vague too. 好的,这个问题有点含糊,因此答案似乎也有点含糊。

Let's have an example to help visualise. 让我们有一个示例来帮助可视化。 You have a newspaper website, so you serve lots of articles - but all from one dynamic page... 您有一家报纸网站,因此可以提供许多文章-但所有文章都来自一个动态页面...

/articles.do?Article=Important-News-Item
/articles.do?Article=Cash-Strapped-Business-Goes-Into-Administration

And so on. 等等。

You then add some .htaccess (mod_rewrite) love to the site... 然后,您向站点添加一些.htaccess(mod_rewrite)爱...

/Articles/Important-News-Item/
/Articles/Cash-Strapped-Business-Goes-Into-Administration/

And you rewrite those URIs back to the articles.do page. 然后将这些URI改写回article.do页面。

So in your articles.do page, you use the request string (Article=Value) to query the Article table and get back the relevant page... 因此,在articles.do页面中,您可以使用请求字符串(Article = Value)查询Article表并返回相关页面...

SELECT
   Headline,
   Content,
   Image
FROM
   Article
WHERE
   RestfulId = 'Important-News-Item'

And then you pop that onto the page. 然后将其弹出到页面上。

Shout if I've missed the point on this question. 如果我错过了这个问题的重点,请大声喊叫。

mod_rewrite example mod_rewrite示例

This is the mod_rewrite rule for the example above, it maps the restful URI back to the articles.do URI. 这是上面示例的mod_rewrite规则,它将静态URI映射回article.do URI。

RewriteEngine on
RewriteRule ^Articles/([^/\.]+)/?$ articles.do?Article=$1 [L,NC,QSA]

The easiest way to explain this (not the technical way to explain this) is: 解释此问题的最简单方法(不是解释此问题的技术方法)是:

1) The first bit (^Articles/) looks for any address that starts with "Articles/" 2) The second bit (([^/.]+)/) represents "Whatever comes between the "/" after Articles and the next "/" after that, which in this example is the title of the article. 1)第一位(^ Articles /)查找以“ Articles /”开头的任何地址。2)第二位(([[^ /。] +)/)表示“ Articles和然后是下一个“ /”,在此示例中为文章标题。

You can capture more parameters by adding more "([^/.]+)/" segments to the end... 您可以通过在末尾添加更多“([[^ /。] +)/”段来捕获更多参数。

RewriteEngine on
RewriteRule ^Articles/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ articles.do?Article=$1&$2=$3 [L,NC,QSA]

In this example you could convert 在此示例中,您可以转换

/Articles/Important-News-Item/My-Key/My-Value/

into 进入

articles.do?Article=Important-News-Item&My-Key=My-Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM