Link's path in modular web design - php

I'm testing modular web design with PHP and relative paths, as follows:
<?php include('top.php'); ?>
<?php include('header.php'); ?>
<!-- Page content -->
<?php include('footer.php'); ?>
So it is easy to create the structure of new pages.
In top.php I use relative paths to link stylesheets and scripts, as follows:
<link type="text/css" rel="stylesheet" href="css/style.css" />
But becomes a problem when I want use the same page structure in pages from child directories. top.php, header.php, and footer.php are the same always. In child directories, I use for instance:
<?php include('../top.php'); ?>
<?php include('../header.php'); ?>
<!-- Page content -->
<?php include('../footer.php'); ?>
But this is wrong, because relative paths for links (inside the includes).
How I can write correctly the links inside the includes to use them in any child directory without problems?
I know that I can use absolute paths of the form http://www.site.com/path/file, but I don't know the www.site.com url yet. How I can make PHP create absolute paths regardless of the domain?
How I can do this correctly?
Sorry for the spelling, I'm not native english speaker.

You should use the __DIR__ constant which points to the location of the file which includes the others. Then use relative(!) paths to the files that you are about to include. Like this:
require_once(__DIR__ . '/../include.php');

Related

PHP - structuring folders, paths, templates

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:
<?php
// This is base.php
define('ROOT_PATH', realpath(dirname(__FILE__)));
Now, in each of your site pages like (site_1.php, subfolder1/site_3.php, etc.) include the base.php file:
<?php
// This is site_1.php
require_once 'base.php';
// ...
In subfolder site pages, include it like this:
<?php
// This is subfolder1/site_3.php
require_once '../base.php';
// ...
If deeper site page, include it like this:
<?php
// This is subfolder1/subfolder2/site_5.php
require_once '../../base.php';
// ...
Now, in any php file, if you want to include another php file (like _nav.php), you can include it like this:
<?php
// This is _header.php
require_once ROOT_PATH . '/_nav.php';
// ...
?>
<!-- to include any css/js/images files, it would be like this: -->
<link rel="stylesheet" type="text/css" href="/css/my_css.css">
<script src="/js/my_js.js"></script>
<img src="/images/my_image.jpg">
I didn't test the code, but it should work.
What you're looking for is the HTML base tag. This will make every relative link use the specified base.
ie. having this in your head:
<head>
<base href="http://www.yoursite.com/assets/" />
</head>
...will make the following code load the image from the assets folder:
<img src="image.jpg" />
See: https://www.w3schools.com/tags/tag_base.asp

Relative URL issue

I will have multiple folders/modules to access common files. But accessing them seems to be big deal for me!
I did gone through this link to understand the relative positioning and managed to solve some . But not all. Reference: Relative URL's/paths in php
My folder structure is as below:
Website runs on root folder:
/(index|ajax).php
and then the subfolders:
/css/style.css
/img/*.(jpg|png|gif)
/inc/(header|footer).php
/js/*.js
/registration/(ajax|getsubjects|response|success).php
Now, this is how I included files in the index.php page(this displays correctly, meaning, style,css,js,config all accessible)
<?php
include('inc/header.php');
?>
content here
<?php
include('inc/footer.php');
?>
This index page will have to fetch getsubjects.php, response.php and then finally land in success.php.
The success.php need some styling whereas the previous two were only for processing.
So now in the success.php I access header and footer as below:
include('../inc/header.php');
include('../inc/footer.php');
But this doesn't apply any styling!
inside header.php and footer I include files like this:
<link rel="stylesheet" href="./css/style.css">
<script src="./js/script.js"></script>
How should I include the files here please?
./css/style.css means from current directory and would achieve the same result as css/style.css. The easiest answer is to determine what the base path of your application is and use that. For instance, if your application is running as http://myapp.com, then you could set all your front-end paths to /css/style.css. If your app runs in a subdirectory, such as http://example.com/myapp, then your paths would be /myapp/css/style.css.
This does not apply the same on the PHP side. For them, you should really use document-relative paths. Having a PHP file that you include in multiple places in your app, the contents of which having something like include('../myDoc.php');, can lead to complications as the path isn't based on the included document's path, but rather the including. So using document-relative paths, you get around this include(__DIR__ . '/../myDoc.php');. Just something to consider if your app grows.
Your PHP-includes seem to be correct. But in your HTML you need to change the linking to the CSS and JS Files (maybe even to your images).
You could use absolute paths:
<link rel="stylesheet" href="/css/style.css">
<script src="/js/script.js"></script>
the leading dot makes your paths relative to the HTML-Document, so if they are linked from a document in a subfolder, they point to a wrong location.
Including files with
<?php
include("page1.php")
?>
put the code (or content) from page1 into the caller page.
So you may have to detect from where your pages are called, or try absolute links (beginning by /)
I hope I answer you question correctly.

Include files from parent to other directories

I am building a website and has index.php in the root folder. The other pages are up to 2 folders deep and separated out by their different categories.
So the index.php file would be correct;
<?php include ('/inc/head.php'); ?>
But the other pages would have to be;
<?php include ('../inc/head.php'); ?> or <?php include ('../../inc/head.php'); ?>
But this breaks the relative paths for the css and script files in the includes files themselves as they then don't map correctly! Any solutions?
You're talking about paths set in head.php? Why not just use absolute paths? So rather than using css/style.css you'd do /css/style.css, etc.
Okay, so I was getting confused and setting the relative paths in the footer and head sections as well as the 'includes'.
So the solution for the head and footer sections are,
<script src="../../js/bootstrap.min.js" type="text/javascript"></script>
Should be set as an absolute path as #ultranaut said:
<script src="/js/bootstrap.min.js" type="text/javascript"></script>
and the include, if the page is two folders deep from the root should then be:
<?php include ('../../inc/head.php'); ?>
Hope this helps some other PHP newbie too!

including php files in a header that is itself included in files in different folder levels

OK, the title is a little confusing but this is the structure of my web site:
/
|--header.php
|--page1.php
+--css
|--style.css
+--subsection
|--page2.php
+--functions
|--classes.php
Right, so both page1.php and page2.php will include header.php. This I do by just using the relative file path from whatever php page to the header.php.
header.php itself includes other files; for example css/style.css and functions/classes.php.
I might include them like the following:
<link rel="stylesheet" href="css/style.css">
<?php require_once("functions/classes.php"); ?>
This will work for page1.php as the reletive paths are correct. For subsection/page2.php this will fail as the paths should be ../css/style.css and ../functions/classes.php but remain as they are defined in header.php.
My question is how can I get header.php to always use the correct relative file paths for its includes regardless of where the file calling header.php (e.g. apage.php) is located in the web site directory.
Set a base path to the css /functions :
define('CSS_BASE','insert/full/path/here');
then access css in header.php using
<link rel="stylesheet" href="<?=CSS_BASE;?>/style.css">
For php includes, making the path relative to header.php is ok.
For the css file, you should use an absolute path, like <link rel="stylesheet" href="/css/style.css">.
Edit : The css/javascripts files are not "include" ; those are html tags, so they will be processed client-side by the browser. That's the reason why the path should be relative to the url of the page where the html will be, not to its place on the server.
When I did this I set a variable (say $urlStart) at the start of each page (page1.php, page2.php) that contained the relative path of the main directory (eg page1.php would have $urlStart='./', page2.php would have $urlStart='../'). Then in your header.php, use this variable at the start of each URL. Like this:
<link rel="stylesheet" href="<?php echo $urlStart;?>css/style.css">
<?php require_once($urlStart . "functions/classes.php"); ?>
If you are including page2.php inside the page1 then your path will work. no need to go back to the previous folder.

Parent directories and PHP

I really hope there's a simple solution to this.
<?php include("header.php"); ?>
Let's say I have a php header in my root folder simply titled header.php. In that header there is a link back to the home page, main.php, and main.php is also located on the root. No problem so far. Here's what header.php looks like. Simple stuff, right?
<link href="style.css" rel="stylesheet" type="text/css" />
<div id="headerwrap">
<div id="linkbox">
<img src="images/mainlogo.png" />
</div><!-- End linkbox -->
</div>
However, let's say I have other pages in subdirectories. Subpage.php is located in a child directory of the root, and so it has to look back to the root to get the included header.php.
<?php include("../header.php"); ?>
That wouldn't be a big deal, except that header.php links back to main.php and also style sheets, none of which are in *subpage.php's directory, thus causing an error when someone on Subpage tries to get back to Main via the link in the header.
I'm just hoping there's a simple way to make this work, and that I don't have to copy and redirect all includes into every subdirectory. Also, there are too many pages to really reasonably include them all on the root. Sorry if this answer is posted elsewhere; I've looked and just have no real idea what I'm looking for. Thanks for your help. Hope all that makes sense.
You could just hard code main.php's path within header.php:
<img src="http://website.com/images/mainlogo.png" />
As opposed to a php prob this seems to be an html prob..
Your links should be relative links with a preceding / i.e.
Text
instead of
Text
how about using absolute links. header.php should also reference main.php absolutely, then there should be no troubles:
<?php include($_SERVER['DOCUMENT_ROOT'].'/header.php"); ?>
You can use the base html tag:
<base href="http://yoursite.com/" />
This way you can use that url as the base for all your links/stylesheets/images and you don't need to worry if they're in a subdirectory.
the best thing to do is to get in the habit of using
$_SERVER['DOCUMENT_ROOT']
that way you have no confusion as to what directory you're in, etc.
so including your header for example would be as simple as :
include $_SERVER['DOCUMENT_ROOT'] . "/header.php";

Categories