PHP include best practices question

前端 未结 10 1655
一生所求
一生所求 2020-11-29 19:40

I have been learning syntax for PHP and practicing it. I come from a .NET background so masterpages always made things pretty easy for me when it came to headers and footer

相关标签:
10条回答
  • 2020-11-29 19:53

    I like using functions to print headers and footers instead of includes. You can fine tune the variable scope better that way.

    0 讨论(0)
  • 2020-11-29 20:00

    What you're doing is ok until you start using "Views" or "Templates" in which case you no longer arrange your content HTML inside the "controller" or "action" running.

    Instead you will load a view and populate it with values which leaves all the HTML source ordering to the view and not your PHP file.

    $view = new View('layout.php');
    $view->header = $header;
    $view->content = 'This is the main content!';
    $view->footer = $footer;
    print $view;
    

    which then loads the layout file which looks something like this:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            ...
        </head>
        <body>
            <div id="header"><?php print $header; ?></div>
            <div id="content"><?php print $content; ?></div>
            <div id="footer"><?php print $footer; ?></div>
        </body>
    </html>
    
    0 讨论(0)
  • 2020-11-29 20:03

    The accepted answer is form 2010 and things have changed in the past ten years.

    The way to go, now with composer having replaced most hand wired autoloaders, the best practice is to use a single require_once utilizing __DIR__ from a script in a fixed, known place:

    require_once __DIR__ . '/vendor/autoload.php';
    

    Using define() is not common anymore.

    According to the environment agnostic approach, dependencies from the environment get injected to the application using .env or similiar.

    0 讨论(0)
  • 2020-11-29 20:05

    The good practice nowadays is to use a templating engine, such as smarty. For the whole application consider using a framework, like codeigniter.

    0 讨论(0)
  • 2020-11-29 20:06

    To summarize all the above.
    That's good way to use includes, but do not forget to use a template page for the page contents.

    Partly based on Galen's and Balus':

    page.php

    require $_SERVER['DOCUMENT_ROOT'].'/../config.php';
    $data = get_data(); // assume we get all required data here.
    $pagetitle = "This is a sample page";
    $template = "page.tpl.php";
    include "main.tpl.php";
    

    main.tpl.php

    <!DOCTYPE html> 
    <html lang="en"> 
        <head> 
             <title><?php echo $pagetitle?></title>
        </head> 
        <body> 
            <?php include $template ?> 
        </body> 
    </html> 
    

    page.tpl.php something like this:

    <h1><?php echo $pagetitle?></h1>
    <?php if (!$data): ?>
    No news yet :-(
    <?php else: ?>
    <ul>
    <? foreach ($data as $row): ?>
    <li><a href="news.php?id=<?php echo $row['name']?>"><?php echo $row['name']?></a></li>
    <?php endforeach ?>
    </ul>
    <?php endif ?>
    
    0 讨论(0)
  • 2020-11-29 20:07

    I include my views from my controllers. I also define file locations to make maintenance easier.

    config.php

    define('DIR_BASE',      dirname( dirname( __FILE__ ) ) . '/');
    define('DIR_SYSTEM',    DIR_BASE . 'system/');
    define('DIR_VIEWS',     DIR_SYSTEM . 'views/');
    define('DIR_CTLS',      DIR_SYSTEM . 'ctls/');
    define('DIR_MDLS',      DIR_SYSTEM . 'mdls/');
    define('VIEW_HEADER',   DIR_VIEWS . 'header.php');
    define('VIEW_NAVIGATION',   DIR_VIEWS . 'navigation.php');
    define('VIEW_FOOTER',   DIR_VIEWS . 'footer.php');
    

    Now i have all the info i need just by including config.php.

    controller.php

    require( '../config.php' );
    include( DIR_MDLS . 'model.php' );
    
    $model = new model();
    if ( $model->getStuff() ) {
        $page_to_load = DIR_VIEWS . 'page.php';
    }
    else {
        $page_to_load = DIR_VIEWS . 'otherpage.php';
    }
    
    include( VIEW_HEADER );
    include( VIEW_NAVIGATION );
    include( DIR_VIEWS . $page_to_load );
    include( VIEW_FOOTER );
    
    0 讨论(0)
提交回复
热议问题