Plateforme Php: CodeIgniter & Smarty
Dans ce tutoriel nous allons donc intégrer Smarty pour CodeIgniter afin de faciliter le développement d’une application WEB.
Installation
Nous allons télécharger WampServeur, CodeIgniter et Smarty.
Pour WampServeur, suivez les instructions d’installation.
Pour CodeIgniter et Smarty il suffit de dezipper les fichiers.
Dans WampServeur nous allons créer notre dossier de base de notre site
C:/wamp/www/tutorielwaanser
Intégration de Smarty
Dans le fichier index.php ajouter la constante : WEBSITE_PATH
if (strpos($system_folder, '/') === FALSE) {
if (function_exists('realpath') AND @realpath(dirname(__FILE__)) !== FALSE)
{
$system_folder = realpath(dirname(__FILE__)).'/'.$system_folder;
}
} else {
// Swap directory separators to Unix style for consistency
$system_folder = str_replace("\\", "/", $system_folder);
}
define('WEBSITE_PATH', realpath(dirname(__FILE__)).'/'.$application_folder );
Dans le fichier system/application/config/config.php modifier la variable :
$config['subclass_prefix'] = 'WAANSER_';
Créer les arborescences suivantes :
- system/smarty/libs et copier les dossiers Internals et plugins de Smarty
system/application/smarty/plugins
system/application/smarty/tpl
system/application/tmp/smarty/cache
system/application/tmp/smarty/templates_c
Renommer le fichier Smarty.class.php en Smarty.php
Copier les fichier Smarty.php, Smarty_Compiler.class.php et Config_File.class.php dans le dossier system/libraries
Dans le fichier system/libraries/Smarty.php couper les constantes et copier les dans le fichier application/config/constants.php
// ******* Smarty ********
define('DIR_SEP', DIRECTORY_SEPARATOR);
define('SMARTY_DIR', BASEPATH.'smarty'.DIRECTORY_SEPARATOR);
define('SMARTY_CORE_DIR', BASEPATH.'smarty/libs/internals' . DIRECTORY_SEPARATOR);
define('SMARTY_PHP_PASSTHRU', 0);
define('SMARTY_PHP_QUOTE', 1);
define('SMARTY_PHP_REMOVE', 2);
define('SMARTY_PHP_ALLOW', 3);
Ensuite nous devons créer les libraries WAANSER_controller et WAANSER_smarty dans le dossier system\application\libraries
class WAANSER_Controller extends Controller {
/**
* @desc for template
* @var obj smarty
*/
public $oMySmarty = null;
public function Controller() {
parent::Controller();
$this->loadSmarty();
}
public function loadSmarty() {
$this->oMySmarty = &load_class('Smarty');
}
}
Ce fichier va nous permettre d’étendre le contrôleur d’origine, pour récupérer tous le processus de chargement des classes, et nous ajoutons un attribut $oMySmarty qui sera un objet de la classe Smarty.
Avec cet objet on pourra changer de générateur de template.
On peut, même si c’est moche
utiliser le générateur de templates par défaut en même temps que le générateur Smarty.
class WAANSER_Smarty extends Smarty {
/**
* @desc class constructor
* @version 1.0.0
* @date 20090307
*/
public function __construct() {
parent::Smarty();
$this->setParams();
}
/**
* @desc initialise les variables pour smarty
* @version 1.0.0
* @date 20090307
*/
public function setParams() {
$this->template_dir = APPPATH.'smarty/tpl';
$this->compile_dir = APPPATH.'tmp/smarty/templates_c';
$this->plugins_dir = array(APPPATH.'smarty/libs/plugins', APPPATH.'smarty/plugins');
}
}
?>
Il nous reste a changer le fichier welcome dans le dossier system\application\controllers, il suffit juste de changer l’appel au fichier parent.
class Welcome extends WAANSER_Controller {
function Welcome()
{
parent::Controller();
}
function index()
{
$this->oMySmarty->assign('TEST_WAANSER', 'Smarty est OK');
$this->oMySmarty->display('welcome.tpl');
//$this->load->view('welcome_message');
}
}
Et de lui ajouter une variable avec la methode assign et de lui indiquer le template a afficher avec la methode display.
Ensuite ajouter le fichier welcome.tpl dans le dossier system\application\smarty\tpl
<p>{$TEST_WAANSER}</p>
Au prochain Tuto


(2 votes, moyenne: 4,00 max : 5)
juin 1st, 2009 at 19 h 22 min
Merci déjà pour l’ensemble de ces tuto, cependant je ne comprends pas très bien à quel moment la class « WAANSER_Smarty » est appelée.
De plus » $this->plugins_dir = array(APPPATH.’smarty/libs/plugins’, APPPATH.’smarty/plugins’) » me semble incorrect car « smarty/libs/ »se trouve dans « system/ ». Il n’aurait pas fallu remplace le premier « APPPATH » par « BASEPATH »?
Merci
juin 1st, 2009 at 19 h 57 min
Autant pour moi, je n’ai pas fait attention au :
« $config['subclass_prefix'] = ‘WAANSER_’ » qui explique l’appel de la class « WAANSER_Smarty » à la place de « Smarty lors du « load_class(‘Smarty’) »
Sorry
juin 24th, 2009 at 20 h 54 min
Bonjour,
Je vous remercie pour ce tuto.
Je l’ai suivi mais lorsque je tente d’accéder à ma page d’accueil j’ai ce message :
Fatal error: Class ‘Controller’ not found in E:\www\[...]\application\libraries\Controller.php on line 3
Quelqu’un à une petite idée ???
merci d’avance,
Steven
juin 24th, 2009 at 21 h 17 min
Désolé je suis un boulet ^^
Le nom de fichier pour les librairies perso ce n’est pas :
application\libraries\Controller.php
mais:
application\libraries\WAANSER_Controller.php
Encore désolé pour ce double post
octobre 19th, 2009 at 16 h 45 min
Hello,
c’est un bon tuto, sur CodeIgniter et Smarty.
J’utilise les 2 aussi depuis des années,
avec une implémentation différente.
Mais celle-ci est très bien
J’aurai une petite question, pourquoi créer une constante dans l’index
« define(‘WEBSITE_PATH’, realpath(dirname(__FILE__)).’/’.$application_folder ); »
qui ne servira a rien, pour la mise en place du système proposé?
Merci,
Cordialement,