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

Asked

Viewed 770 times

2

Filing cabinet 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 each *.php where there is a need for the page to have the menu, the best way is to make a include? For example:

  • Let’s assume that we are in the root directory and we are in the index.php of this same directory, my own include would be on this path: include_once("Templates/menu.php");

Now, if I create a directory called home and create a index.php within it (home/index.php), the path of include above would stand: include_once("../Templates/menu.php");, which would take a lot of work.

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

I was thinking about doing OOP.

  • 1

    Yes. This is the best way if you are not using MVC.

  • With MVC, you can make the view always render the header and footer, in addition to the content you need, so you’ll need to give this include only once. Otherwise, you can set constants with the path and include the constant.

  • 3

    The way you have shown it is already an "expert" way. rsrr You will have to do the includes anyway, with or without mvc or Oop. By the way, Oop and mvc have nothing to do with it. rsr

  • Have you searched for templates engine? : - Twig - Mustache - Smarty

  • If you do include with absolute path, you have no problem worrying about which directory is the file that uses include. include_once( "$raiz_do_site_no_hd/Templates/menu.php" ); - Another good thing is to avoid capitalization on names. Do a DOCUMENT_ROOT search for more details on how to get the absolute path from the site root automatically.

1 answer

3

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

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

Example:

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

Inside the folder elementos, 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 file index.php, you will have your page settings. You can use a folder called paginas to add other php pages, but which will be dynamically included in index.php

In the archive helpers.php, you will include some functions that helped you in the project.

For example, to facilitate the loading of 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>

Browser other questions tagged

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