问题描述
我正在做一个网页项目.为此,我决定使用 Apache、PHP(5.1.7,由我的服务提供商强加的版本)和 Dwoo(模板).
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.
我想将 URL 路由到我的模板.我知道有很多框架在做这种事情.我只是想知道是否有一种很好的方法来实现它.
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.
我的项目设置如下:
- src/dwoo - Dwoo 文件
- index.php - 这应该处理路由.目前它只是使用模板呈现网站的首页.
- 模板 - 代表实际页面的模板.
业务逻辑最少(没有真实模型).这只是非常静态的页面.使用模板使维护工作更容易(继承即.).
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.).
知道在这种情况下如何设置路由吗?我想理想情况下,每个给定的 URL 都应该通过 index.php 路由,然后以某种方式决定要呈现哪个模板(即/category/pagename 将映射到 templates/category/pagename.tpl).
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).
推荐答案
使用 mod_rewrite
将所有内容路由到单个 index.php
文件.然后检查此文件中 $_SERVER['REQUEST_URI']
中的变量以分派到所需的处理程序.
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.
此配置将启用 mod_rewrite
,如果它已安装:
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]
这篇关于PHP 中的路由 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!