Créer un module

Créer vos pages

Dernière mise à jour : 15/01/2019 à 21h56
Si vous êtes familier avec la Programmation orientée objet et le modèle MVC vous ne devriez pas avoir beaucoup de problèmes pour créer vos pages.

Le fichier index.php




Le fichier index.php devra être créé à la racine de votre module. C'est ce fichier qui va vous permettre d'utiliser tel et tel Contrôleur selon l'url.

Voici un exemple :

Code PHP :
<?php
/*##################################################
 *                                 index.php
 *                            -------------------
 *   begin                : September 06, 2012
 *   copyright            : (C) 2012 Kévin MASSY
 *   email                : kevin.massy@phpboost.com
 *
 *
 ###################################################
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ###################################################*/
define('PATH_TO_ROOT', '..');
require_once PATH_TO_ROOT . '/kernel/init.php';
$url_controller_mappers = array(
    new UrlControllerMapper('MyModuleAdminController', '`^/admin/?$`'),
        //Contrôleur exécuté lorsqu'il n'y a pas de paramètre
    new UrlControllerMapper('MyModuleHomeController', '`^/?$`')
);
DispatchManager::dispatch($url_controller_mappers);
?>


Lorsque vous allez accéder à votre module avec cette url http://localhost/my_module/?url=/admin, la classe DispatchManager va exécuter le Contrôleur MyModuleAdminController (Lorsque la réécriture des urls est activée, ?url= ne sera plus utile).

Vous pouvez également gérer vos paramètres, par exemple, dans le cas où vous souhaitez supprimer une news, il vous faudra transmettre l'identifiant de la news.

Exemple :

Code PHP :
<?php
/*##################################################
 *                                 index.php
 *                            -------------------
 *   begin                : September 06, 2012
 *   copyright            : (C) 2012 Kévin MASSY
 *   email                : kevin.massy@phpboost.com
 *
 *
 ###################################################
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ###################################################*/
define('PATH_TO_ROOT', '..');
require_once PATH_TO_ROOT . '/kernel/init.php';
$url_controller_mappers = array(
    new UrlControllerMapper('MyModuleAdminController', '`^/admin/?$`'),
        //Contrôleur exécuté lorsqu'il n'y a pas de paramètre
    new UrlControllerMapper('MyModuleHomeController', '`^/?$`'),
        //Contrôleur exécuté avec l'url /my_module/?url/edit/2
        new UrlControllerMapper('EditNewsController', '`^/edit/([0-9]+)?/?$`', array('identifier'))
);
DispatchManager::dispatch($url_controller_mappers);
?>


Il vous faudra quelques notions de Regex pour mettre en place la gestion des paramètres.


Les Contrôleurs




Veuillez d'abord, pour une meilleure clarté du module, créer un dossier controllers à la racine de votre site.

Créer ensuite un fichier et une classe, par exemple MyModuleHomeController. La classe doit être une classe fille de ModuleController ou AdminModuleController (selon si c'est un module à exécuter dans l'administration ou sur le site).

Une fonction est obligatoire dans cette classe :

Code PHP :
public function execute(HTTPRequestCustom $request)
    {
    }


Elle devra retourner une Response :

- SiteDisplayResponse si vous souhaitez afficher votre page avec le design du site
- SiteNodisplayResponse si vous souhaitez afficher votre page sans le design du site
- AdminMenuDisplayResponse si vous souhaitez afficher votre page avec le design de l'administration et un menu à gauche pour les différentes pages de votre module
- AdminDisplayResponse si vous souhaitez afficher votre page avec le design de l'administration sans le menu de gauche
- AdminNodisplayResponse si vous souhaitez afficher votre page sans le design de l'administration

La Response ne se charge que de la vue (ce que vous voyez).

Vous devez donc lui donner en paramètre une vue que vous allez afficher dans votre module (objet Template).

Exemple :

Code PHP :
<?php
/*##################################################
 *                         MyModuleHomeController.class.php
 *                            -------------------
 *   begin                : September 06, 2012
 *   copyright            : (C) 2012 Kévin MASSY
 *   email                : kevin.massy@phpboost.com
 *
 *
 ###################################################
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ###################################################*/
class MyModuleHomeController extends ModuleController
{
    public function execute(HTTPRequestCustom $request)
    {
        $tpl = new StringTemplate('Voici le page d\'accueil de mon module!');
        return $this->build_response($tpl);
    }
    private function build_response(View $view)
    {
        $response = new SiteDisplayResponse($view);
        $response->get_graphical_environment()->set_page_title('Mon premier module !');
        return $response;
    }
}
?>


Cas particulier : JSONResponse




Vous avez également la possibilité de renvoyer des données en JSON dans un contrôleur vous permettant de communiquer plus facilement entre différents langages de programmation comme le Javascript par exemple.

Exemple :

Code PHP :
<?php
/*##################################################
 *                         MyModuleJSONController.class.php
 *                            -------------------
 *   begin                : September 06, 2012
 *   copyright            : (C) 2012 Kévin MASSY
 *   email                : kevin.massy@phpboost.com
 *
 *
 ###################################################
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 ###################################################*/
class MyModuleJSONController extends ModuleController
{
    public function execute(HTTPRequestCustom $request)
    {
        $data = array('Vos données');
        return new JSONResponse($data);
    }
}
?>
Cette page a été vue 8413 fois