MrJ
MrJ

Reputation: 1938

Hard-coding URLs vs Nested Set vs Combo in Content System

I've been putting together a database to handle content produced for a site, however, thinking about the long-term, I'm unsure if I have the best system.

At present I'm using the routing method of passing everything via index.php which .htaccess routes as follows index.php?route=example/url (user sees http://www.domain.com/example/url)

At present the database is setup like below:

uid            | title        | content | category
--------------------------------------------------
/              | Home         | aaa     | 1
/example       | Example      | bbb     | 2
/example/url   | Example: URL | ccc     | 2

Though I am not sure if this is the best approach, especially if I wanted to rename example to something - I'd have to rename each URL...

So I've also thought about the Nested Set method (such as http://www.phpclasses.org/package/2547-PHP-Manipulate-database-records-in-hierarchical-trees.html) though this would just show lots of different numbers in the database where I could access everything by it's node. Example below;

node | left | right | name
--------------------------
1    | 1    | 6     | Home
2    | 2    | 5     | Example
3    | 3    | 4     | URL

Then I could use the node as the uid? But I'm unsure how I could translate http://www.domain.com/example/url to the uid equalling 3...

I already do have a category column in my database at the moment, to categorise the content, though I could potentially alter this.

I'm basically looking for suggestions about how to proceed, because as the site gets more content it will be harder to change the setup - so I want to ideally get this right from day one.

If anyone can give some help/suggestions - I'd be grateful!

Upvotes: 1

Views: 168

Answers (2)

Fge
Fge

Reputation: 2961

The nested set model probably is a good choice here. That'd result in a table layout like (id,left,right are the fields required by the nested set model, the others contain the respective content):

| id | left | right | uid | title | content | category |

More details on how to perform a particular query can be found here.

However I would not perform the look up on the database but a simple array cache:

new array('/' => array('content' => 'aaa', 'category' => 'bbbb'),
          '/example/' => array(),
          .....
);

This cache can be build up very easy (though expensive) and queried very easy.

On a side note: i suspect you're trying to model page content here. Maybe you should refactor you database structure then as this table would have two responsibilities (url->content mapping and content).

Upvotes: 0

Tieson T.
Tieson T.

Reputation: 21201

Well, if you use index.php?route=example/url, you could always do something like this:

$args = explode( '/', $_GET['route'] );
$args = filter_var_array( $_GET['route'], FILTER_SANITIZE_STRING );

Then your values of $args would be:

0 -> example
1 -> url

etc. You could then use these values to determine what template to load, and what content to grab from the database, or whatever else you're doing already.

HTH.

Upvotes: 1

Related Questions