PHP include best practices question

前端 未结 10 1738
一生所求
一生所求 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 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 );
    

提交回复
热议问题