PHP - structuring folders, paths, templates - php

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

Related

how to setup the assets folder in CodeIgniter like in Laravel

like the public folder given by laravel default i tried to create something like that for easy make up of my css, js, picture and so on. I tried configuring it like this. In my application/helpers/utility_helpers.php
<?php
function asset_url()
{
return base_url().'assets/';
}
?>
I have my assests folder along with application, system, user_guide etc.
i confugured the autoload function of application/config as :
$autoload['helper'] = array('url','utility');
when i hit any url in browser now, it says
An Error Was Encountered
Unable to load the requested file: helpers/utility_helper.php
how can i make it and can you explain me if i need to specify root for that helpers also.And if not can i simply access the assets like this:
<img src="assets/base_components/build/banner.png">
in my view
In fact the answer is very simple, you only need to put ../ before every file that is under assets folder, because the root of the codeigniter is in application folder, but how you can't put the assets folder there and for the best practices, let the structure of the project like below and apply this solution to the path of the files:
Website Folder structure
|---|application\
|-------(CodeIgniter application folders...)
|---|assets\
|-------css\
|-------images\
|-------js\
|-------robots.txt
|---|system
|-----(CodeIgniter system folder...)
|index.php
Path of the files
ex.:
CSS
<link href="../assets/css/bootstrap.min.css" rel="stylesheet">
<link href="../assets/css/plugins/font-awesome.css" rel="stylesheet">
IMAGES and others
<img src="../assets/images/build/banner.png">
JAVASCRIPT
<script src="../assets/js/jquery-2.1.4.js"></script>
<script src="../assets/js/plugins/jquery.scrollSpeed.js"></script>
Note.:
Always remember to applay the best practices to the code too (this will help for the performance of the the page too), like below:
<html>
<head>
meta tags
favicons
css files styles
</head>
<body>
page structure code
javascript files
</body>
</html>
Yo can do this instead:
<img src="<?= base_url('assets/base_components/build/banner.png') ?>">
Your code looks ok, it should work.
According to the error
Unable to load the requested file: helpers/utility_helper.php
there is no utility_helper.php file in application/helpers/ folder. Check if the file exists in that folder or if you don't have a typo in the file name.

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!

Link's path in modular web design

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');

PHP Include links

Hello I'm having my first serious go with PHP to create a sample script for my self. It has a basic structure, in my root folder I have:
index.php
core folder (holds most of my php function files)
includes (holders my header.php and footer.php)
sites - (sites has 3 further folders site A, B, C)
CSS
js
All pages are made up by taking a header.php and footer.php from the includes folder and then each page has its own content in the middle. The header.php contains (as well as basic html and links to javascripts stylesheets ect) includes from core folder like so:
include_once '/core/connect.php';
Now these all work great using the index.php which provides links to the 3 different sections of the site, sitea, siteb and sitec.
But when you navigate out of the document root to say /sites/sitea/index.php all those links are now broken.
What is the best way to go about building the links in the header.php section so they are relative site wide no matter which folder you are in?
The idea behind this is that you do only have ONE file for each process.
So process all pages through index.php
index.php would contain, for example,
require('header.php');
include('content.php');
require('footer.php');
That way, it won't break the site if your content doesn't show.
Your index is always loaded from the same path, so header/footer wouldn't change. Just content.
When you're including you want to use a real path, not a relative path...
require_once ($_SERVER['DOCUMENT_ROOT'].'/includes/header.php');
/* something happens here */
require_once ($_SERVER['DOCUMENT_ROOT'].'/includes/footer.php');
The best way is to always use physical path from wherever you are - this way every page that include other page with includes won't get break:
PHP 5.2 and down:
require(dirname(__FILE__) . '/core/connect.php');
PHP 5.3 and above
require(__DIR__ . '/core/connect.php');

Categories