Organization of files in PHP

3

I have the following folder structure.

   classificados
   index.php
   login.php
     -->config
          -->conecta.php
     -->funcoes
          -->banco-usuario.php
     -->headers
          -->cabecalho.php
          -->menu.php
          -->rodape.php
     -->front
          -->painel.php
          -->outrasPaginas.php

If I include in the file pane.php the code so require_once("../headers/cabecalho.php"); goes from error always, because the file header.php that is in the headers folder, calls the file menu, then the file panel.php does not find the menu, wanted to do it the right way, how do I do it?

Can anyone help me?

    
asked by anonymous 01.03.2015 / 14:43

1 answer

1

One way to work with paths is to define their constant% with a path with the path to the root directory of your project. From here you do not have to worry about where your script is.

<?php

define('ROOT_PATH', '../'); // para os arquivos em um nível em relação a raiz
                            // para subpastas adicione alguns '../' extras

require ROOT_PATH . 'config/conecta.php';
require ROOT_PATH . 'headers/cabecalho.php';

Remember that require depends on current file location . If you are in the require file, knowing that both are in the same directory, you can use the relative path between the files:

File cabecalho.php

<?

define('ROOT_PATH', '../');

require 'menu.php'; // Irá incluir o arquivo headers/menu.php

require ROOT_PATH . 'headers/rodape.php'; // Irá incluir o arquivo headers/rodape.php
                                          // Porém antes ele irá subir um diretório e 
                                          // voltar para headers em seguida
                                          // 'headers/../headers/rodape.php'
    
02.03.2015 / 12:19