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 http://www.domain.com/account/ from http://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 http://www.domain.com/account/home instead of http://www.domain.com/module/account/home.php, and http://www.domain.com/module/user/register.php would actually be accessed by http://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:
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.
EDIT:
To also allow files that end in “.php” to be served from the module directory, add the following to your .htaccess file: