Jump to content

Recommended Posts

Hi All,

I have been messing around with some of my projects and have discovered what i assume is quite a regular "issue" in projects.

When i have a file in a folder, not in the same folder as most of webpage files, including the header is leading to some issues.

For example, if the layout is as follows
 

My Project
- home.php
- index.php
	- includes
		- header.php
	- testing
		- test.php
	- style
		- custom.css

if i include the header on every page with

include 'includes/header.php';

and the header includes the style with

include './style/custom.css';

on every page that i create in this new testing folder, which is a copy of a page that might exist elsewhere, i am having to change the path of the includes.

I have started to look at using the below (which doesnt follow my file strucure)

// Predefined path.
define('PATH', '/home/ftpuser/public_html/includes/');
include(PATH . 'myscript.php');

i understand what is going on here.

 

Long winded, but my question is.  Is the a standard to where these paths should be stored.  I dont want to put them in every file, the header file doesnt seem right.  Should i create a new file for things like this "paths.php"?

Secondly, would people recommend the above or superglobal paths with:

// Superglobal path.
include(dirname(__FILE__) . '/myscript.php');

I know that the answer to this is likely going to be put them whereever you want, personal preference etc but i am looking for a bit of advice coming from experience about where these would be put in your projects.

Thirdly, how would i go about linking css and js files in plain html using these paths, is this possible without opening php inside the link?

Thanks as always in advance and have a great new year!

Edited by Adamhumbug
Link to comment
https://forums.phpfreaks.com/topic/314372-where-to-store-predefined-paths/
Share on other sites

  • Adamhumbug changed the title to Where to store predefined paths

Don't you want to use two dots to reference the styles folder if you are already down in the includes folder? 

Personally I store my 'project' stuff in a specific folder for that project and don't have to worry about all that.  If I really needed to separate project-specific files into their own sub folders then that is exactly what I would have - sub-folders under the project-specific folder rather than folders in a parallel structure.  And if I have certain files that are global to much of my work (such as a db connection file) those I store in an upper level folder (outside of the web-accessible tree) where all my projects can reference them with a root-level path conjoined to that folder.

I would try to minimize the number of PHP scripts which are publicly accessible and typically just have a single index.php in my public folder, and would locate header.php outside of the document route.  This way, you don't have to add all those silly "if something is not defined, die not accessible"  It also makes the paths easy to deal with relative url's to your client resources and you can hardcode them in your HTML if you wish.

YourProject/
    public/
        index.php (routes to the appropriate part of the application)
        style/
            custom.css
        js/
            custom.js
        images/
            custom.png
    src/
        YourClasses.php
    testing/
        test.php
    vendor/ (if you currently don't use composer, I recommend you learn about it)
    template/
        includes/
            header.php
    config/
        config.ini (or json, etc)

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.