tags:

views:

19

answers:

1

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. 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. Without getting into code snippets, I'm simply looking for the logic on how this is generally handled.

A: 

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...

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

And you rewrite those URIs back to the articles.do page.

So in your articles.do page, you use the request string (Article=Value) to query the Article table and get back the relevant page...

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

This is the mod_rewrite rule for the example above, it maps the restful URI back to the articles.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.

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
Sohnee
No, actually I think you've got it. Basically, I think what I need to do is to redo my mod_rewrite rules to handle this. Are you any good at mod_rewrite? Can I post it for you to look at? I don't think that it is set up to work that way.
jim
I will add a quick mod_rewrite example to my answer to show you how to get started...
Sohnee