帮助引导和正则表达式为PHP [英] Help with bootstrap and regex for PHP

查看:222
本文介绍了帮助引导和正则表达式为PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个新的PHP框架,在今后的项目个人使用,并  下面是我的计划文件结构至今。我只是需要一些帮助,一些​​正则表达式我的.htaccess文件以及一些帮助我怎么可以加载我想要的文件。

基本上,任何文件夹域名后,应该加载从我的模块文件夹中。  我想有它的 www.domain.com/module/account / 加载 www.domain.com/account / 。我也希望它在格式的任何其他文件夹,我有以下模块。 模块下的所有文件夹/文件应加载就好像它是在顶层。

在这个例子中,虽然在我的模块/帐号/文件夹,如果我有一个名为home.php文件,那么我应该能够与 www.domain.com/account/home ,而不是访问对 www.domain.com/module/account/home.php www.domain.com/module/user/register.php 实际上是由 www.domain.com/user/register

我希望这是有道理的和AP preciate任何帮助和任何意见。我主要是需要用.htaccess文件,使这个文件夹结构的工作有所帮助。我还没有决定是否所有文件都不过一个索引文件进行访问,或者我应该只包括引导类型的文件到每一页。该引导文件将设置的所有变量,配置选项,以及自动加载所有的类文件,并创建需要的对象。

  myFramework /
 - /资产/
-------- / CSS /
-------- /图片/
-------- / JavaScript的/
 -  /包括/
--------- /班/
--------- /配置/
--------- /语言/
--------- / header.php文件
--------- / footer.php
 -  /模块/
 -  -  -  - /帐户/
---------------- / create.php
---------------- / login.php中
---------------- / logout.php
---------------- /的settings.php
---------------- / editprofile.php
-------- /管理/
-------- /博客/
-------- /论坛/
-------- /邮件/
-------- /用户/
--index.php
 

解决方案

从jasonbar其实答案几乎没有。它所缺少的是像你描述的处理.php扩展名:

  RewriteEngine叙述上
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{REQUEST_URI}!^ /模块
的RewriteCond%{REQUEST_URI} [] PHP $
重写规则(。*)[] PHP $ /模块/ $ 1
 

话虽这么说,我强烈建议你考虑一个前端控制器模式(如你在你的问题描述躲避到),因为这样做允许更大的控制,鼓励MVC方法等= O)

编辑:

我纠正了一些被忽视的要点,并添加了PHP扩展的正确处理。需要注意的是[L]参数在年底会导致进一步的处理停止,使得这些code块,在你的.htaccess文件的逻辑结构有用的(由$ P $即pventing遵循任何处理);如果这样的功能不希望删除的说法。

我还添加了一行专门检查所请求的PHP文件确实存在。

  RewriteEngine叙述上

#如果URI相匹配的模块目录的目录中,重定向到。关闭
#这一块,如果你不希望有任何目录浏览或有
#默认的Apache文件加载。
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{DOCUMENT_ROOT} /模块%{REQUEST_URI} -d
的RewriteCond%{REQUEST_URI}!^ /包括
的RewriteCond%{REQUEST_URI}!^ /资产
的RewriteCond%{REQUEST_URI}!^ /模块
重写规则(。*)/模块/ $ 1 [L]

#如果URI文件SAN的PHP扩展的模块目录相匹配,
#然后重定向到这一点。
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{DOCUMENT_ROOT} /模块%{REQUEST_URI}的.php -f
的RewriteCond%{REQUEST_URI}!^ /包括
的RewriteCond%{REQUEST_URI}!^ /资产
的RewriteCond%{REQUEST_URI}!^ /模块
重写规则(。*)/module/$1.php [L]
 

编辑:

要还允许结束.PHP从模块目录服务的文件,添加以下到你的.htaccess文件:

 #如果URI匹配与模块目录.php为扩展名的文件,
#然后重定向到这一点。
的RewriteCond%{} REQUEST_FILENAME!-d
的RewriteCond%{} REQUEST_FILENAME!-f
的RewriteCond%{DOCUMENT_ROOT} /模块%{REQUEST_URI} -f
的RewriteCond%{REQUEST_URI}!^ /包括
的RewriteCond%{REQUEST_URI}!^ /资产
的RewriteCond%{REQUEST_URI}!^ /模块
#请注意,以下行限制只能访问PHP文件。注释掉
#以下行,以便在模块导演的任何现有的文件是
#访问(或修改以下允许其他文件扩展名被读取)
的RewriteCond%{REQUEST_URI} [] PHP $
重写规则(。*)/模块/ $ 1 [L]
 

I am working on a new PHP framework for personal use in future projects, and below is my planned file structure so far. I just need some help with some regex for my .htaccess file and some help for how I can load the files I want.

Basically, any "folder" after the domain should load from my "module" folder. I would like to have it load www.domain.com/account/ from www.domain.com/module/account/. I also want it in that format for any other folder I have under modules. All folders/files under "module" should load as if it were in the top level.

In this example though in my module/account/ folder, if I have a file called home.php then I should be able to access it with www.domain.com/account/home instead of www.domain.com/module/account/home.php, and www.domain.com/module/user/register.php would actually be accessed by www.domain.com/user/register

I hope this makes sense and appreciate any help and any advice. I mainly need help with the .htaccess file to make this folder structure work. I have not decided if all files should be accessed though a single index file or if I should just include a bootstrap type file into every page. The bootstrap file would set up all variables, config options, as well as auto load all class files and create objects needed.

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

解决方案

The answer from jasonbar is actually almost there. All it lacks is dealing with the .php extension as you described:

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

That being said, I'd strongly encourage you to consider a front controller paradigm (as you eluded to in your problem description) as doing so allows for much greater control, encourages an MVC approach, etc. =o)

EDIT:

I corrected a few neglected points and added proper processing of the PHP extension. Note that the [L] argument at the end causes further processing to cease, making these code blocks useful as logical structures within your .htaccess file (i.e. by preventing any processing that follows); remove that argument if such functionality is not desired.

I've also added a line to specifically check that the php file being requested actually exists.

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]

EDIT:

To also allow files that end in ".php" to be served from the module directory, add the following to your .htaccess file:

# 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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