在PHP中的URL路由 [英] Routing URLs in PHP

查看:104
本文介绍了在PHP中的URL路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在网页上的项目。我决定使用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.

我已经建立了我的项目如下:

I've set up my project as follows:


  • 的src / dwoo - Dwoo文件

  • 的index.php - 这应该处理路由。目前,它只是呈现使用模板网站的头版。

  • 模板 - 这重新present实际页面模板

有是业务逻辑的最小量(没有真正的模型)。这一切都只是pretty静态页面。使用模板使维护工作变得更加容易(即继承)。

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然后以某种方式决定要渲染的模板路径(即/分类/网页名称将映射到模板/分类/ 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路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆