⊗ppPmSDFl 437 of 447 menu

File-based engine in PHP

Let's implement a site engine that will allow us to create a template file, to which, depending on the URL, different content will be connected.

Let the structure of our page look like this:

<!DOCTYPE html> <html> <head> <title>title</title> </head> <body> <header> header </header> <main> content </main> <header> footer </header> </body> </html>

Let's insert a command we made up for content insertion in the place where the content should be inserted, for example like this: {{ content }}. Let's change our site template:

<!DOCTYPE html> <html> <head> <title>title</title> </head> <body> <header> header </header> <main> {{ content }} </main> <header> footer </header> </body> </html>

Now let's create a view folder, where we will place content files. Right in this folder or in subfolders.

The first file will be like this:

<div> content 1 </div>

The second file will be like this:

<div> content 2 </div>

The third file will be like this:

<div> content 3 </div>

Now let's make it so that the corresponding file is pulled from the view folder based on the URL from the address bar. In our case, the URL /page1 will correspond to the first file, the URL /dir/page2 - to the second, and the URL /dir/sub/page3 - to the third.

Let's start the implementation. First, in the .htaccess file, let's make sure all requested addresses, except resource files, are redirected to the index.php page:

RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} !\.(js|css|ico|jpg|png|gif)$ RewriteRule .+ index.php

On the index.php page, let's get the requested URL into a variable:

<?php $url = $_SERVER['REQUEST_URI']; ?>

Then, let's get the text of the template file:

<?php $layout = file_get_contents('layout.php'); ?>

Now, based on the URL, let's get the corresponding content file from the view folder:

<?php $content = file_get_contents('view' . $url . '.php'); ?>

Let's replace our made-up command in the template text with the content obtained from the file:

<?php $layout = str_replace('{{ content }}', $content, $layout); ?>

Let's output to the browser the template file with the substituted content:

<?php echo $layout; ?>

Let's put it all together and get the following code:

<?php $url = $_SERVER['REQUEST_URI']; $layout = file_get_contents('layout.php'); $content = file_get_contents('view' . $url . '.php'); $layout = str_replace('{{ content }}', $content, $layout); echo $layout; ?>

Implement the described file-based engine.

Srpski
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEnglishEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
Koristimo kolačiće za rad sajta, analitiku i personalizaciju. Obrada podataka se vrši u skladu sa Politikom privatnosti.
prihvati sve podesi odbij