php - 帮助 PHP 的 Bootstrap 和正则表达式

标签 php regex .htaccess bootstrapping

我正在开发一个新的 PHP 框架供个人在未来的项目中使用,并且 下面是我到目前为止计划的文件结构。我只需要一些关于我的 .htaccess 文件的正则表达式的帮助,以及一些关于如何加载我想要的文件的帮助。

基本上,域之后的任何“文件夹”都应该从我的“模块”文件夹中加载。 我想让它从 www.domain.com/module/account/ 加载 www.domain.com/account/。对于我在模块下的任何其他文件夹,我也希望它采用这种格式。 “模块”下的所有文件夹/文件都应该像在顶层一样加载。

在这个例子中,虽然在我的 module/account/文件夹中,如果我有一个名为 home.php 的文件,那么我应该能够使用 www.domain.com/account/home 访问它www.domain.com/module/account/home.phpwww.domain.com/module/user/register.php 实际上会被 www.domain.com/user/register

我希望这是有道理的,感谢任何帮助和建议。我主要需要 .htaccess 文件的帮助才能使此文件夹结构正常工作。我还没有决定是否应该通过单个索引文件访问所有文件,或者我是否应该只在每个页面中包含一个 Bootstrap 类型的文件。 Bootstrap 文件将设置所有变量、配置选项,以及自动加载所有类文件并创建所需的对象。

myFramework/
--/assets/
--------/css/
--------/images/
--------/javascript/
--/includes/
---------/classes/
---------/config/
---------/language/
---------/header.php
---------/footer.php
--/module/
--------/account/
----------------/create.php
----------------/login.php
----------------/logout.php
----------------/settings.php
----------------/editprofile.php
--------/admin/
--------/blog/
--------/forums/
--------/messages/
--------/users/
--index.php

最佳答案

jasonbar的答案其实差不多了。它所缺少的只是处理您所描述的 .php 扩展名:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/module
RewriteCond %{REQUEST_URI} [.]php$
RewriteRule (.*)[.]php$ /module/$1

话虽如此,我强烈建议您考虑使用前端 Controller 范例(正如您在问题描述中所回避的那样),因为这样做可以实现更好的控制,鼓励使用 MVC 方法等。=o)

编辑:

我更正了一些被忽略的点并添加了对 PHP 扩展的正确处理。请注意,末尾的 [L] 参数会导致进一步处理停止,从而使这些代码块可用作 .htaccess 文件中的逻辑结构(即通过阻止随后的任何处理);如果不需要此类功能,请删除该参数。

我还添加了一行来专门检查请求的 php 文件是否确实存在。

RewriteEngine On

# if the uri matches a directory in the module dir, redirect to that. Disable 
# this block if you don't wish to have either directory browsing or to have the 
# default apache file load.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/module%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} !^/includes
RewriteCond %{REQUEST_URI} !^/assets
RewriteCond %{REQUEST_URI} !^/module
RewriteRule (.*) /module/$1 [L]

# if the uri matches a file sans the .php extension in the module directory, 
# then redirect to that.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/module%{REQUEST_URI}.php -f
RewriteCond %{REQUEST_URI} !^/includes
RewriteCond %{REQUEST_URI} !^/assets
RewriteCond %{REQUEST_URI} !^/module
RewriteRule (.*) /module/$1.php [L]

编辑:

要同时允许从模块目录提供以“.php”结尾的文件,请将以下内容添加到您的 .htaccess 文件中:

# if the uri matches a file with the .php extension in the module directory, 
# then redirect to that.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/module%{REQUEST_URI} -f
RewriteCond %{REQUEST_URI} !^/includes
RewriteCond %{REQUEST_URI} !^/assets
RewriteCond %{REQUEST_URI} !^/module
# note that the following line restricts access to php files only. comment out 
# the following line to allow any existing file under module director to be 
# accessed (or modify the following to allow other file extensions to be read)
RewriteCond %{REQUEST_URI} [.]php$  
RewriteRule (.*) /module/$1 [L]

关于php - 帮助 PHP 的 Bootstrap 和正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4640038/

相关文章:

JavaScript 正则表达式使用可能的通配符子域说明符检查 URL

php - 使用 Mysql 和 PHP 统计类别列表中的帖子数量

JavaScript正则表达式测试函数: *OR* syntax

javascript - 如何在javaScript中刷新页面后更新数据库中表的星级?

regex - R中的字母数字正则表达式

wordpress - "Deny from all"但仍然允许 WordPress 访问文件?

regex - htaccess 正则表达式除了某些文件类型

php - Laravel 4.2 中路由的 HTTPS 错误 - Assets 通过 HTTPS 加载正常

php - 具有不同值和排序值的 MongoDB geoNear

php - 在 MySQL 中是否可以只更新第 n 次出现?