The best way to organize menu and footer in php is with with includes? [closed]

2

File menu.php :

<nav class="navbar navbar-default">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="#"><img src="/imagens/logo.png"></a>
        </div>
        <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#">Page 1</a></li>
            <li><a href="#">Page 2</a></li>
            <li><a href="#">Page 3</a></li>
        </ul>
    </div>
</nav>

Let's say I have to add the menu in every * .php where there is a need for the page to have the menu, the best way is to do an include? For example:

  • Suppose we are in the root directory and we are in index.php of this same directory, my include would be in this path: include_once("Templates/menu.php");

Now, if I create a directory named home and create a index.php inside it (home / index.php), the path of include above would be: include_once("../Templates/menu.php"); , which would work hard.

Is there any simplified way to make an include of menu , footer etc more "expert"?

I was thinking of doing in OOP .

    
asked by anonymous 21.01.2016 / 16:07

1 answer

2

Carlos, basing my knowledge on working with frameworks, I suggest you build a framework to work better with PHP.

For example, I think you should organize your project by separating everything in folders.

Example:

app/
   index.php
   elementos/
   paginas/
   helpers.php

Within the elementos folder, I would put all that is only partial (menus, footers, sidebar, reusable search forms and etc.).

Example:

 app/
    elementos/menu.php
    elementos/footer.php

In your index.php file, you will have the settings for your page. You can use a folder called paginas to add other pages in php, but they will be included dynamically in index.php

In the helpers.php file, you will include some functions that will help you with the project.

For example, to facilitate loading the "elements" we could create a function there.

 function element($element)
 {
     return include __DIR__ . '/elementos/' . $element . '.php';
 }

So you can do something similar in your index.php file

  <?php include __DIR__ . '/helper.php'; ?>

  <html>
        <body>
           <div><?php element('menu') ?></div>
           <div><?php page($_GET['page']) ?></div>
           <div><?php element('menu') ?></div>
        </body>
  </html>
    
29.04.2016 / 03:21