Reputation: 37
I have been setting up a simple framework for myself to use for projects with PHP.
My basic folder structure is currently as follows from the root:
Because I'm storing all of my pages in their own directory but the index.php just outside of the pages directory, what I am essentially looking for is a way to get the absolute path to the index.php that I can then store in a variable and call for any links such as the stylesheet or javascripts within the site.
So my question basically is just how to do that. I am currently using MAMP as my localhost environment, feel free to ask me about any other information regarding my question that I might have missed.
Upvotes: 1
Views: 1402
Reputation: 4757
From the look of it, your index.php would be also where you set your Apache DocumentRoot?
If I understood correctly, you can use PHP's $_SERVER variable and the one you would be interest in is $_SERVER["DOCUMENT_ROOT"]
On the other hand, you mentioned you wanted to use this variable for CSS and Javascript files? From a client side it would be impossible to use an absolute file path but you would rather need a relative path (because the client aka browser, have no visibility on your web server's file system).
If you are serious about this framework, I would also suggest looking into design patterns such as MVC. And also consider implementing a FrontController to dispatch all your requests. This would give you more control on paths and how you parse your files.
Upvotes: 1