Organization of PHP files

Asked

Viewed 289 times

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 the code in the.php panel file like thisrequire_once("../headers/cabecalho.php"); goes from the error always, because the.php header file that is in the headers folder, calls the menu file, then the panel.php file does not find the menu, wanted to do it the right way, as I do it?

Can someone help me?

  • 1

    use the absolute path, avoid going back var/www/html/headers/menu.php is not the best way to do this, use some Fw to take care of those details for you

  • Have you ever thought about MVC?

  • Already Herme, but I haven’t learned yet, so I’m doing as it gives hehehe

  • 1

    Use a framework, adopt XYZ architecture... often we don’t need it. For learning purposes I think the path of stones is super valid.

1 answer

1

One way to work with the paths is to define before your require a constant with the path to the root directory of your project. From there you don’t have to worry 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 the require depends on the current file location. If you’re in the file cabecalho.php, knowing that both are in the same directory, you can use the relative path between the files:

Filing cabinet headers/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'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.