I have the following codes:
I have my index.php
<!DOCTYPE html>
<html>
<head>
<?php include('includes/head.php');?>
</head>
<body>
<?php include('includes/navbar.php');?>
<?php include('includes/footer.php');?>
</body>
</html>
head.php
<title>Titulo</title>
<link rel="stylesheet" type="text/css" href="../css/estilos.css">
<link rel="stylesheet" type="text/css" href="../css/estilos.css">
<link rel="stylesheet" type="text/css" href="../css/jquery.dataTables.css">
<link rel="icon" href="../img/favicon.ico" type="image/x-icon"/>
footer.php
<script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/estilos.js"></script>
<script src="js/jquery.dataTables.js"></script>
<script src="js/buttons.js"></script>
<script src="js/html5.min.js"></script>
<script src="js/buttons.print.min.js"></script>
<script src="js/row.js"></script>
<script src="js/jquery.table2excel.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script src="js/listaSoftware.js"></script>
I also have a navbar but there is no point in putting it here.
This all works in the index, but if I create a folder called views and create a file home.php and
home.php
<!DOCTYPE html>
<html>
<head>
<?php include('../includes/head.php');?>
</head>
<body>
<?php include('../includes/navbar.php');?>
<?php include('../includes/footer.php');?>
</body>
</html>
This home finds the head and footer but they no longer find the JS and CSS. my directory
-css
-js
-includes
--footer.php
--navbar.php
--head.php
-view
--home.php
index.php
For example, the Index works fine, but when I want to call home.php, which is inside the view folder, it does not find the paths of the js and css.
What am I doing wrong and I hope I explained myself well.
The problem is that being in the view folder, the interpreter expects the javascript files to be in
example.com/view/js/...
You can declare the
src
one of the scripts so that they always point to the root, in this wayAnd the same with the css
When doing the search
<?php include('../includes/footer.php');?>
forhome.php
the directories within the folder,view
for example<script src="js/jquery-3.3.1.min.js"></script>
, it is searching within the folder in which these directories do not exist, to access you have to leave a directory, for example,<script src="../js/jquery-3.3.1.min.js"></script>
as a solution you can have two filesfooter.php y footer2.php
since when performing the include takes the path where the file was includedIn this case, the ideal is to have a configuration file that indicates the
BASE_DIR
(base directory of the project) and theBASE_URL
(base of the routes from the browser).~/config.php
Predefined constants in PHP
Include the configuration file from each of the files, the path must be written according to the level of folders where it is located, like this:
~/index.php
require_once __DIR__ . '/config.php';
~/view/home.php ~/includes/*.php
require_once __DIR__ . '/../config.php';
Change PHP paths (from any file that includes the config.php)
Change HTML paths (must include config.php file)