I have a question about templating my php project.
Currently my folder structure looks like this:
/Root
|index.php
|_footer.php
|_header.php
|_nav.php
|
|site_1.php
|site_2.php
|
|css/
|js/
|images/
|subfolder1
|site_3.php
|site_4.php
|subfolder2
|site_5.php
I usually include "_header" and "_footer.php" in my index.php file, but I have problems with subfolder pages. For example, my _header.php has an include "_nav.php". But when I include the _header.php in the site_4.php I get the problems with the assets and navigation.
I read to make a config file to define site url, asset paths etc, but It's not so clear to me. Ideally I would like to have an "assets" folder with subfolders for css, js etc.
Also I would like to know who can I with include "_nav.php" with site_url(). I tried that out, but I always get errors. The main question is, how to make the site_url, base url (at this point I'm still confused about those terms and how to use them) Thank you.
Because you're using pure PHP (no framework being used) this is a bit tricky. There are several ways to solve this, I'd suggest the following:
In your Root folder, create new file, let's name it "base.php", which has the content of:
Now, in each of your site pages like (site_1.php, subfolder1/site_3.php, etc.) include the base.php file:
In subfolder site pages, include it like this:
If deeper site page, include it like this:
Now, in any php file, if you want to include another php file (like _nav.php), you can include it like this:
I didn't test the code, but it should work.