Problems with PHP routes

1

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.

All this 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 these no longer find the JSy 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 that is inside the view folder it does not find the paths of the js and css.

In what I am failing and I hope to have explained myself well.

    
asked by Luis Fernando 26.01.2018 в 21:43
source

3 answers

2

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 of the scripts so that they always point to the root, in this way

<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>

And the same with the css

<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">
    
answered by 26.01.2018 / 21:57
source
0

At the time of making <?php include('../includes/footer.php');?> in home.php look for the directories inside the folder view for example <script src="js/jquery-3.3.1.min.js"></script> is looking inside the folder in which these directories do not exist, in order to access you have to exit a directory for example <script src="../js/jquery-3.3.1.min.js"></script> as a solution you can have two files footer.php y footer2.php since when doing the include take the path in which the file was included

    
answered by 26.01.2018 в 21:56
0

For this case the ideal is to have a configuration file that indicates the BASE_DIR (base directory of the project) and the BASE_URL (base of the routes from the browser).

~ / config.php

<?php

/** Ruta del proyecto */
declare('BASE_DIR', __DIR__);

/** Ruta en el navegador */
declare('BASE_URL', 'http://localhost/nombre-proyecto');
  

It is because the routes are called referring to the current file for this reason you must precede the routes with the PHP predefined constant __DIR__ otherwise the route from where the file is included would start.

PHP predefined constants

Include the configuration file from each of the files, the route must be written according to the level of folders where it is, like this:

~ / index.php

require_once __DIR__ . '/config.php';

~ / view / home.php ~ / includes / *. php

require_once __DIR__ . '/../config.php';

Change PHP routes (from any file that includes the config.php)

include BASE_DIR . '/includes/navbar.php';
include BASE_DIR . '/includes/head.php';
include BASE_DIR . '/includes/footer.php';

Change HTML paths (must include the file config.php)

<link rel="stylesheet" type="text/css" href="<?= BASE_URL . '/css/estilos.css' ?>">

<script src="<?= BASE_URL ?>/js/listaSoftware.js"></script>
  

Routes with figure <?= BASE_URL ?> only work if you have assets short_open_tag otherwise you must replace it with <?php echo BASE_URL ?>

    
answered by 26.01.2018 в 22:10