Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

apache - Routing URLs in PHP

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...