8

I'm working on a web page project. I decided to use Apache, PHP (5.1.7, version imposed by my service provider) and Dwoo (templating) for this purpose.

I want to route URLs to my templates. I'm aware there are many frameworks doing this kind of thing. I'm just wondering if there's a nice way to achieve it without.

I've set up my project as follows:

  • src/dwoo - Dwoo files
  • index.php - This should handle routing. Currently it just renders the front page of the site using a template.
  • templates - Templates that represent actual pages.

There is minimal amount of business logic (no real model). It's all just pretty static pages. Using templates makes maintenance work easier (inheritance ie.).

Any idea how to set up routing in this case? I guess ideally each given URL should route via index.php that then somehow decides which template to render (ie. /category/pagename would map to templates/category/pagename.tpl).

Juho Vepsäläinen
  • 26,573
  • 12
  • 79
  • 105

3 Answers3

10

Use mod_rewrite to route everything to a single index.php file. Then check the variable in $_SERVER['REQUEST_URI'] within this file to dispatch to the required handler.

This configuration will enable mod_rewrite, if it's installed:

DirectorySlash Off
Options FollowSymLinks Indexes
DirectoryIndex index.php

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}  -d
RewriteRule  ^.*$  -  [L]

RewriteCond %{REQUEST_FILENAME}  -f
RewriteRule  ^.*$  -  [L]

RewriteRule ^.*$    index.php [L]
troelskn
  • 115,121
  • 27
  • 131
  • 155
  • Thanks. Is the "Options FollowSymLinks Indexes" part needed? My host seems to fail with it (Internal Server Error). Without it I get "The requested URL /appname/index.php was not found on this server" while trying to access some page other than index. Perhaps it might be just neater to forget Apache and figure out how to render the templates to HTML ~before~ I upload anything to the server... – Juho Vepsäläinen May 23 '11 at 13:15
  • `FollowSymlinks` is only needed if you use symlinks. It's probably not strictly needed. `Indexes` *is* needed, as otherwise requesting `/` won't render `index.php`. When you get `Internal Server Error` do look in Apache's error log to see what the problem is. – troelskn May 23 '11 at 20:23
  • Rendering the files offline is a good idea for several reasons, but it can actually get quite complex to do. If you want this, I would suggest that you go with an existing package, such as Jekyll. – troelskn May 23 '11 at 20:30
  • Ok. I gave it a go just for the kicks. I managed to make it to generate HTML just fine. Now I just need to resolve some path issues (relative CSS and such). :) – Juho Vepsäläinen May 24 '11 at 08:06
  • 2
    "Indexes is needed, as otherwise requesting / won't render index.php." - this is plain wrong. Indexes enables the directory listing if no index file is found. DirectoryIndex is the directive which makes index.php usable – cweiske May 26 '11 at 08:03
2

Like trolskn (+1) describes:

Use mod_rewrite to route everything to a single index.php file. Then check the variable in $_SERVER['REQUEST_URI'] within this file to dispatch to the required handler.

But I found the following .htaccess (placed in the folder with the index.php which should "consume" everything after it) much more helpful:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

Also I would like to note that you might encounter the message

.htaccess: Invalid command 'RewriteEngine', perhaps misspelled
or defined by a module not included in the server configuration

This can easily be solved with sudo a2enmod rewrite && sudo service apache2 restart (source)

Community
  • 1
  • 1
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
1

You might want to use PEAR's Net_URL_Mapper.

cweiske
  • 30,033
  • 14
  • 133
  • 194