8

我正在做一个网页项目。为此,我决定使用 Apache、PHP(5.1.7,我的服务提供商强加的版本)和 Dwoo(模板)。

我想将 URL 路由到我的模板。我知道有很多框架在做这种事情。我只是想知道是否有一种很好的方法来实现它。

我的项目设置如下:

  • src/dwoo - Dwoo 文件
  • index.php - 这应该处理路由。目前它只是使用模板呈现网站的首页。
  • 模板 - 代表实际页面的模板。

业务逻辑数量最少(没有真实模型)。这只是非常静态的页面。使用模板使维护工作更容易(即继承)。

知道在这种情况下如何设置路由吗?我猜理想情况下,每个给定的 URL 应该通过 index.php 路由,然后以某种方式决定要呈现哪个模板(即 /category/pagename 将映射到 templates/category/pagename.tpl)。

4

3 回答 3

10

用于mod_rewrite将所有内容路由到单个index.php文件。然后检查此文件中的变量$_SERVER['REQUEST_URI']以分派到所需的处理程序。

如果已安装,此配置将启用mod_rewrite

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]
于 2011-05-23T10:28:53.113 回答
2

就像 trolskn (+1) 描述的:

用于mod_rewrite将所有内容路由到单个index.php文件。然后检查此文件中的变量$_SERVER['REQUEST_URI']以分派到所需的处理程序。

但我发现以下内容.htaccess(放置在index.php应该“消耗”其后所有内容的文件夹中)更有帮助:

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

另外我想指出您可能会遇到该消息

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

这可以通过sudo a2enmod rewrite && sudo service apache2 restart( source )轻松解决

于 2015-05-22T16:47:41.750 回答
1

您可能想使用 PEAR 的Net_URL_Mapper

于 2011-05-23T17:53:59.083 回答