I have the following structure of my project (watch image). I need include menu.php in each file but index.php need a different path:
index.php: include('../contenido/menu.php');
agregar.php: include('../../contenido/menu.php');
How can I solve that issue? I do not use a framework.
Set the include path based on the file in question.
You can add a relative path using the __DIR__
magic constant.
For example, in index.php
...
set_include_path(implode(PATH_SEPARATOR, [
__DIR__ . '/../contenido',
get_include_path()
]));
and in your vistas/productos
scripts...
set_include_path(implode(PATH_SEPARATOR, [
__DIR__ . '/../../contenido',
get_include_path()
]));
Then, it doesn't matter where you are, you can simply add
include 'menu.html';