5

I am currently whipping up a very basic CMS for a personal project of mine. It is as much for my own education as anything. One question I have is how do I achieve url's / permalinks without file extentions. I understand using get variables to pull data from the DB but how do you convert this to something like www.url.com/posttitle instead of something like www.url.com/?posttitle='blablabla.

Also on a slightly different topic can anyone point me in the direction of an EASY to use framework for developing sites that deal with memberships and member listings eg craigslist.

I currently develop within wordpress and am quite capable but am less familiar with OOPHP and custom CMS development from a base level.

Thanks in advance for any help or suggestions.

2

3 Answers 3

9

You'd use a .htaccess file to pass all requests to your front controller (which is usually just an index.php script) and then that script matches the incoming request to a record in your database.

For example, if you had a database table called pages with four columns: id, title, slug and content, the following would be a simple implementation…

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1 [NC,L]

This tells Apache to take all requests that aren't a file or a direction and send them to index.php.

Your index.php could then look as follows:

<?php
// Take request URI
// Would be something like 'your-slug'
$request = trim($_SERVER['REQUEST_URI'], '/');

// Set up database connection and attempt to match slug
$sql = "SELECT * FROM pages WHERE slug = ? LIMIT 1";
$smt = $db->prepare($sql);
$smt->execute(array($request));

$page = $smt->fetchObject();

if (! $page) {
    // Page was not found matching slug
    header('HTTP/1.1 404 Not Found');
    exit;
}

// Display matching page in a template

From here, you can then build upon it.

2

This can be done simply via url rewriting (in .htaccess placed in the root of your website directory structure).

Or you can rewrite everything to your index.php for example and then parse it here.

You just grab the URI part of the url from the $_SERVER variable (take a look at 'QUERY_STRING' or just var_dump($_SERVER) to see which key contains what).

Here is the sample .htaccess file for rewriting everything:

RewriteEngine on
# rewrite everything except for assets to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php [L]
2

File-less extensions are usually the result of an abstraction of the URL path - what this means is that your application should interpret everything after the URL and start presenting data based on that.

Consider the following:

http://www.url.com/about-us

On a normal HTTP request (on Apache, for example), Apache would try to serve a public folder called /about-us, and since it isn't specified, a static index.php file.

With any of the popular MVC frameworks, like CodeIgniter, CakePHP, Ruby on Rails, and so on, /about-us is matched to what is called a route, which loads assets relevant to that page. So instead of loading a static page, it runs out to a database, grabs the data for that page, grabs the template, and dynamically serves the file. That's essentially one way to get "pretty" URLs.

If you're looking to roll your own, I highly recommend any of the above frameworks. Don't just use them without understanding them, though - try to get a feel for what the execution process is. Get a grasp on what exactly each request does.

As far as authentication goes, I know there are a few options in Rails like Devise and CanCan. These are basically pre-coded authentication modules that allow you to easily configure them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.