Search code examples
phppathrelative-pathabsolute-pathphp-include

Including files from different folders after changing the document root


I am working on a project using WAMP. The vhost is configured like this:

<VirtualHost *:80>
   ServerName LearningPHP
   DocumentRoot c:/wamp64/www/learning_php/public/page/
   <Directory  "c:/wamp64/www/learning_php/public/page/">
       Options Indexes FollowSymLinks MultiViews
       AllowOverride All
       Require local
   </Directory>
</VirtualHost>

The Windows host file looks like this:

127.0.0.1       LearningPHP       # Project wamp64/www/learning_php
::1             LearningPHP       # Project wamp64/www/learning_php

The project structure looks like this:

enter image description here

I am trying to achieve this:

  • when I access the website to land on learning_php/public/page/index.php (I think I accomplished this through the vhost configuration);
  • inside each folder under page directory (e.g. login, profile_add etc.) there are two files: index.php and process.php. I used this approach to organize my project better (I am not yet familiarized with the MVC pattern);
  • inside the css and js folders the name of the files matches the name of the folder under the page directory (e.g. page > profile_add => css/profile_add.css or js/profile_add.js). Later on I will merge all css and js files a page needs into individual files to limit the HTTP requests;

The problem I am experiencing:

  • I cannot figure out how to include the css and js files. I think this happens because I rewrite the DocumentRoot in the vhost configuration.

I tried answers from similar questions using PHP constants, but I could not figure out what the issue is. For instance, I tried to add this <link rel="stylesheet" type="text/css" href="../public/css/style.css"> to the index.php under the page folder and it did not work. I also used the file location on disk, but no result.

I would really appreciate any help.


Solution

  • After consulting someone else on this issue I concluded (strictly referring to my own project) that:

    • Having the structure displayed above the css file cannot be access by the browser because it is simply outside the document root specified in the vhost configuration. An alternative is to point the document root to learning_php/public/ and structure the project this way:

    enter image description here

    • another way to accomplish the desired result, but without modifying the project structure, is to read the css files via PHP and then store/ output the content wherever needed.

    The bottom line is that, as far as I could find out, folders outside the document root are not accessible to the browser (which makes sens keeping in mind the security reasons and how most of the PHP frameworks use this principle). However, the same file can be manipulated using server-side scripting.

    Please correct me, if I am mistaken.