Rendre plus lisible mes pages php, votre avis

  • Auteur de la discussion Auteur de la discussion sff
  • Date de début Date de début
WRInaute impliqué
Bonjour,

afin de rendre plus lisible mes pages php, voici ce que j'ai fais :

Auparavant, mon site était contenu dans une seule page php (index.php)

Dedans on y retrouvait l'intégralité de mon site et ca devenait un peu le bordel pour s'y retrouver.

Afin d'augmenter la clarté de cette page index.php, j'en ai crée un nouvelle page.

Dans cette nouvelle page j'ai crée une dizaine de fonction contenant les principale structure de ma page.php

Fonction news()
fonction newsletter()
fonction meteo()
etc

dans la page index.php je fais appel à ces fonction pour afficher les news, newsletter, meteo...

Mon fichier index.php ressemble donc à ceci :

Code:
include('manouvellepage.php');

...

$page .= '<div class="block_section1">'.news().'</div><div class="block_section2">'.meteo().'</div>';

...

echo $page;

et la nouvelle page :

Code:
...

fonction news() {

$page ='...';

return $page;

}

fonction newsletter() {

$page ='...';

return $page;

}

fonction meteo() {

$page ='...';

return $page;

}

...

Qu'en pensez vous ?

Cela vous choque t'il ou cela vous parrait bien ?

J'attends vos avis. Merci
 
WRInaute impliqué
Ok merci, je vais me pencher sur ta première proposition. Les pseudo templates c'est un peu démodé.
 
WRInaute accro
Voilà ce que je met dans le index.php à la racine du site avec ce système.
NB: j'utilise ce système pour les micro sites, sinon c'est Laravel/CakePHP/Django.

PHP:
<span class="syntaxdefault"></span><span class="syntaxkeyword"><?</span><span class="syntaxdefault">php<br />ini_set</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'display_errors'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 1</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">date_default_timezone_set</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'Europe/Brussels'</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxcomment">// Paths<br /></span><span class="syntaxdefault">define</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'DS'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> DIRECTORY_SEPARATOR</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">define</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'BASE_PATH'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> dirname</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">__FILE__</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> DS</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">define</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'INCLUDES_PATH'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> BASE_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'includes'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> DS</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">define</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'DATA_PATH'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> BASE_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'data'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> DS</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">define</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'TEMPLATES_PATH'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> BASE_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'templates'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> DS</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">define</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'PAGES_PATH'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> TEMPLATES_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'pages'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> DS</span><span class="syntaxkeyword">);<br /></span><span class="syntaxcomment">// Urls<br /></span><span class="syntaxdefault">define</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'BASE_URL'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'http://'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> $_SERVER</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'HTTP_HOST'</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> dirname</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$_SERVER</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'SCRIPT_NAME'</span><span class="syntaxkeyword">])</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'/'</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">define</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'ASSETS_URL'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> BASE_URL </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'assets/'</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">define</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'THEMES_URL'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> ASSETS_URL </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'css/themes/'</span><span class="syntaxkeyword">);<br /><br />require_once(</span><span class="syntaxdefault">INCLUDES_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'functions.php'</span><span class="syntaxkeyword">);<br />require_once(</span><span class="syntaxdefault">INCLUDES_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'Template.php'</span><span class="syntaxkeyword">);<br />require_once(</span><span class="syntaxdefault">INCLUDES_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'config.php'</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxdefault">$page </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!empty(</span><span class="syntaxdefault">$_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'page'</span><span class="syntaxkeyword">])</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> $_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'page'</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">:</span><span class="syntaxdefault"> </span><span class="syntaxstring">'index'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$pageFile </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> PAGES_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> $page </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'.php'</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">if </span><span class="syntaxkeyword">(!</span><span class="syntaxdefault">is_readable</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$pageFile</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">||</span><span class="syntaxdefault"> strpos</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$_SERVER</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'REQUEST_URI'</span><span class="syntaxkeyword">],</span><span class="syntaxdefault"> </span><span class="syntaxstring">'//'</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!==</span><span class="syntaxdefault"> false</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">    header</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Status: 404 Not Found"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">    $page </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'404'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">    $pageFile </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> PAGES_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> $page </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'.php'</span><span class="syntaxkeyword">;<br />}<br /><br /></span><span class="syntaxdefault">$lang </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!empty(</span><span class="syntaxdefault">$_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'lang'</span><span class="syntaxkeyword">])</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> $_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'lang'</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">:</span><span class="syntaxdefault"> </span><span class="syntaxstring">'fr'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$locale </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $config</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'languages'</span><span class="syntaxkeyword">][</span><span class="syntaxdefault">$lang</span><span class="syntaxkeyword">][</span><span class="syntaxstring">'locale'</span><span class="syntaxkeyword">];<br /></span><span class="syntaxdefault">$localePath </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> BASE_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'locale'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> DS </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> $locale </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> DS </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'LC_MESSAGES'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> DS</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">putenv</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"LC_ALL=</span><span class="syntaxdefault">$locale</span><span class="syntaxstring">"</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'.utf8'</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">setlocale</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">LC_ALL</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $locale </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'.utf8'</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxdefault">$textdomain </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'messages'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$poFile </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $localePath </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> $textdomain </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'.po'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">if </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">file_exists</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$poFile</span><span class="syntaxkeyword">))</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">    $mtime </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> filemtime</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$poFile</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxdefault">    $textdomainNew </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $textdomain </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'_'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> $mtime</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">    $gettextCompiled </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $localePath </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> $textdomainNew </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'.mo'</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">    if </span><span class="syntaxkeyword">(!</span><span class="syntaxdefault">file_exists</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$gettextCompiled</span><span class="syntaxkeyword">))</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">        exec</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">sprintf</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'msgfmt -cv -o %s %s'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $gettextCompiled</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $poFile</span><span class="syntaxkeyword">));<br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">    bindtextdomain</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$textdomainNew</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> BASE_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'locale'</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">    textdomain</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$textdomainNew</span><span class="syntaxkeyword">);<br />}<br /><br /></span><span class="syntaxdefault">$layout </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> isAjax</span><span class="syntaxkeyword">()</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> </span><span class="syntaxstring">'ajax.php'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">:</span><span class="syntaxdefault"> </span><span class="syntaxstring">'main.php'</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">$template </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> new Template</span><span class="syntaxkeyword">();<br /></span><span class="syntaxdefault">$template</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">page </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $page</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$template</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">title </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!empty(</span><span class="syntaxdefault">$config</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'pages'</span><span class="syntaxkeyword">][</span><span class="syntaxdefault">$page</span><span class="syntaxkeyword">][</span><span class="syntaxstring">'title'</span><span class="syntaxkeyword">])</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> $config</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'pages'</span><span class="syntaxkeyword">][</span><span class="syntaxdefault">$page</span><span class="syntaxkeyword">][</span><span class="syntaxstring">'title'</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">:</span><span class="syntaxdefault"> null</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$template</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">theme </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!empty(</span><span class="syntaxdefault">$_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'theme'</span><span class="syntaxkeyword">])</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">?</span><span class="syntaxdefault"> $_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'theme'</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">:</span><span class="syntaxdefault"> null</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$template</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">config </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $config</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$template</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">content </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $template</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">render</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$pageFile</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxdefault">echo $template</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">render</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">TEMPLATES_PATH </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> </span><span class="syntaxstring">'layouts'</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> DS </span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> $layout</span><span class="syntaxkeyword">);</span><span class="syntaxdefault"></span>
 
WRInaute impliqué
Je suis en train d'essayer de mettre en application le code de cette page :

http://chadminick.com/articles/simple-php-template-engine.html

Le premier code, je le place ou ?

Code:
<?php class Template { private $vars = array(); public function __get($name) { return $this->vars[$name]; } public function __set($name, $value) { if($name == 'view_template_file') { throw new Exception("Cannot bind variable named 'view_template_file'"); } $this->vars[$name] = $value; } public function render($view_template_file) { if(array_key_exists('view_template_file', $this->vars)) { throw new Exception("Cannot bind variable called 'view_template_file'"); } extract($this->vars); ob_start(); include($view_template_file); return ob_get_clean(); } }
 
WRInaute accro
Bah où tu veux, à toi de choisir ta structure. Le code que je t'ai donné ci-dessus en suggère une.
 
WRInaute impliqué
Je n'arrive pas à faire fonctionner et je n'ai surtout pas l'habitude de créer des sites de cette manière.

main.php n'affiche rien
 
WRInaute impliqué
On nous propose un fichier controller.php, main.php et content.php. Je les ai tous mis à la racine de mon site. Le premier code je sais toujours pas quoi en faire et ou le mettre. Bref ca me gonfle. Est ce que tu pourrais me guider pour faire au moins fonctionner cet exemple ?

Merci d'avance
 
WRInaute accro
Perso j'adopte une structuration différente :

Code:
<?php
include("configuration.php");

// initialisation de variables globales
$MENU = "3";
$MENUO = "3";
$DOC_SOCIAL = "";
$DOC_TITLE = "";
$DOC_DESC = ""; 
$DOC_INDEX = "";
$DOC_ARIANE = "";

...

// Traitement de l'URI

// Accès bases de données

// Préparation du menu et du fil d'ariane et du menu de navigation du footer


include("haut.php");

// préparation du contenu 

// affichage du contenu

include("bas.php");     


/******************************************************************************
 * FONCTIONS
 *****************************************************************************/

Et ensuite j'ai plusieurs types de pages. Pour les pages articles c'est une page commune à tous les articles, pour les pages catégorie j'ai une page différente par catégorie et la page index est différente également.

L'orientation vers la bonne page se fait par l'intermédiaire du .htaccess

Je pourrais simplifier en utilisant une seule page pour toutes les catégories mais ça m'obligerait ensuite d'aller lire la base de données pour savoir à quelle catégorie se rapporte l'URL. Comme le nombre de catégories n'est pas immense et ne change pas beaucoup je trouve aussi simple de créer une nouvelle page chaque fois que j'ajoute une catégorie. C'est aussi beaucoup plus souple car je peux avoir des présentations différentes pour certaines catégories. Mais pour du full dynamique ça ne fonctionnerait pas.

J'évite de faire trop d'includes. Chaque include c'est un hit. Ca ralentit l'affichage de la page.
 
Discussions similaires
Haut