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:
root folder
package.json
gruntfile.js
node_modules
src
index.php
pages
about.php
contact.php
partials
header.php
footer.php
js
css
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.
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.
Related
I am currently using SLIM as a PHP framework. I have a slight problem with paths (below is my file hierarchy).
I'd like to be able to have a clean url that presents itself like this: "https://cppoi/home", but by doing this my html includes no longer work as they are supposed to. It's as if they can't see outside their "bubble" or the vhost bubble, which points to "application/". If I however set it to point to the entire project folder, which includes the assets folder in which my stylesheets are located, my includes work again but my url becomes "https://cppoi/application/home" which is uglier.
There is probably a very easy solution to this, thanks for your time.
There is no super easy solution, your app setup doesn't necessarily follow best practices. You would usually setup a public folder that contains only your front controller index.php and any public assets. (+ stuff like .htaccess. This public folder is then exposed via webserver, nothing else. That way your code is not accessible from the web, but your main entry point (front controller) and your assets are.
I saw alot of frameworks like Laravel, Zend, Symfony .... and I noticed that they put php files in the root directory, but when I saw WordPress and vBulletin and alot of famous scripts, and I noticed that they put the php files in the public directory.
I wanna know what is the best place to put my PHP files. Root directory or public_html directory? Which is better? and Why! And what is the difference between them?
Assuming by "root directory" you mean somewhere outside of the web server's document root, and that "public_html" is the web server's document root...
It's best practice to only have scripts that must be directly accessible within your web server's doc root. If something happens where PHP gets disabled, you don't want the whole world downloading copies of your source code. Scripts can include files from wherever they can access on disk, so it's possible to only put a loader in the web server's doc root and keep the whole application out of there. This is common and best practice.
Wordpress likely doesn't do this by default because most folks installing Wordpress don't really know what they're doing. It's easier for them to just extract a tarball to one place and not have to worry about it. The ones that do know what they are doing can customize installation paths if desired.
This question seems really elementary, but I have never had a use for the scenario as of yet, so figure I will get some advice. I am building a complex application with an accompanying API in a LAMP environment.
mydomain.com will be the location of the main service and system.
api.mydomain.com will be the location of all endpoints for my API.
All of the class files, DB config files, etc, will be located at main domain/root folder.
Is there a recommended way to handle this? Or is it as simple as including the required files on the API side/folder?
It all depends on whether api.mydomain.com is on the same server as mydomain.com. If they are, you could allow access to the shared files from both areas by using direct paths. So for example, say you have a structure like this:
/webdata/shared
/webdata/api
/webdata/www
You could simply just do an include like:
include_once('/webdata/shared/config.php');
As it is on the same server, it should be no problem. You should use absolute paths for including, so for example /web/www/htdocs/config.inc.php instead of ../htdocs/config.inc.php, as it would get really confusing.
It would be the best to have a simple pathConf.inc.php in api directory and root directory, containing this absolute path - so you will be able to easily change it afterwards. Then, use this path configured in pathConf.inc.php and include your classes, db configs etc.
I've just started learning about HMVC in CodeIgniter.
So far I've been enjoying having modular controllers, but problems come when I wish to include javascript.
It seems to me that I'll have to include javascript file from the view instead of the widgets which isn't really good because I tend to forget which widgets has to come with which javascript file.
Anyone has a better way to do it?
Assets (css,js,images) you should place outside of application folder so you can access them directly.
Thus , you load them using base_url() to start with, and base_url() remains same from wherever you call it.
if you want to split assets in modules as well, perhaps make an assets folder, which further contains folders with module names, each containing css,js,images files. then use base_url()."/assets/module_name/js/script.js"
or something of the sort
As default, you cannot access files within your module folder as it was protected by .htaccess in applications folder.
To allow access in your assets/public folder within your module folder just add another .htaccess within the folder and add the following line.
Allow From All
I'm trying to include a premade messageboard, Phorum, into one of my Zend projects. Phorum is a relatively large and intricate web of PHP includes. My website already has a signup, so I'm trying to combine the two login systems into one.
What I've done is to make a controller that includes the Phorum index.php. This will let me use the authentication system I've set up. My problem is that, since I'm bootstrapping, all the relative filepaths within Phorum's index.php seem to try to begin at my Zend project's index.php, meaning they all seem to break.
Is there a way around this? Should I include? Render? Something better?
All help is appreciated.
Adding the appropriate chdir (back to Phorums include path root) in your Bootstrap file should do it. Then you have to of course take care that you Zend App uses application specific and not relative paths.
You can use the include_path setting, which can be set at runtime using set_include_path(). This doesn't require you to change the working directory, but makes PHP search for includes in all the directories specified in this setting.