Hey. Almost all sites have common parts that are the same for all pages. My question is how technically more correct and more convenient to do the same parts. Suppose different pages have only the middle of the code, the beginning and end are the same. Then you can:
a) Do nothing, each page will have its own identical code. Then if you need to replace something, you will have to edit all pages of the site. When there are more than 15 of them, this already causes some difficulties.
b) as I usually do:

<body> <?php require('top.php'); ?> код страницы <?php require('footer.php'); ?> </body> 

In principle, this method is good for everyone, but I doubt that it is correct.
c) there were some php functions, I don’t remember which ones, there the whole page was one code, and pieces of code were inserted into unique places, unique places themselves were designated%. I did not like this method, there were some minuses, I don’t remember already.
d) there is still some way, I am sure.
Help me choose the best way to make such a layout.

    2 answers 2

    You can do something like this:

    index.php

     <?php include "router.php"; 

    router.php

     <?php function not_found($path) { header('HTTP/1.0 404 Not Found'); echo "<h1>404</h1>"; die ("page not found! - $path -"); } if (!empty($_SERVER['REQUEST_URI'])) $path=preg_replace('#^/|/$|\.\.#','',$_SERVER['REQUEST_URI']); //fixme подумать насчет секурности здесь. if ($path=="") $path="main"; if (!file_exists(dirname(__FILE__)."/pages/".$path.".php")) not_found($path); include dirname(__FILE__)."/header.php"; include dirname(__FILE__)."/pages/".$path.".php"; include dirname(__FILE__)."/footer.php"; 

    .htaccess (if Apache, for other servers a little bit different and REQUEST_URI in the file above must be changed

     RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^.*$ router.php 
    • pages / main.php - homepage content
    • header.php - header
    • footer.php - footer
    • pages / sample.php - which thread is still content, as you call it so. contact as http://mysite.org/sample/

    the point is the following - we redirect all requests to router.php which decides which file to show in the content.

    The code above is better understood before using. This is an example of how to make a router and not a code that is recommended to copy-paste.

    there is a demo with history.js

    source demo

    • Well, specifically this one - lack of caching on html (you can do it, you can even implement the entire router in .htaccess), a lot more, this is not a full-fledged framework. - zb '
    • Well, not for your sake, I just wrote a demo to showcase history.js, but it also fits here. sekurnost - you need to be sure that with the help of the special created by REQUEST_URI you will not be able to make a request to other files, except from the directory pages - zb '
    • well thank you. One more question: file_exists don't care if something is given to him: C: \ xampp \ htdocs \ archive / pages / other.php? I'm talking about slashes - sinedsem
    • And I do not know this, I do not write under Windows. check out - zb '
    • It works, but still weird ... Well, thank you very much for your help. - sinedsem

    There is one project, attendance of 200,000 per day. It uses the require connection there (because parsing templates with all sorts of% pseudo-tags% is expensive). All of you are doing right.

    • And I think that's basically a clean way. Just each site contains the same piece of code, as required ... - sinedsem
    • usually template engines do compilation of templates, so %% is not expensive. - zb '