Précédent   Forum des professionnels en informatique > PHP > Outils > Zend > Zend Framework
Zend Framework Forum d'entraide sur la programmation PHP avec Zend Framework. Avant de poster -> FAQ ZF, Cours ZF
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 23/08/2011, 15h40   #1
Membre régulier
 
Inscription : août 2008
Messages : 402
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 402
Points : 79
Points : 79
Par défaut Comment charger correctement un plugin externe ?

Bonjour, J'essaye d'utiliser Phpthump mais cela ne fonctionne pas, je pense que je ne maîtrise pas le chargement des classes.

Pour essayer de mieux comprendre son chargement j'essaye de faire un petite exercice,
je donne mon arbre de dossier
Code :
1
2
3
4
5
6
7
8
9
10
11
 
racine
|-- application
  |-- controller
    |--indexController
|--library
  |--zend
  |--Charge
    |--LoadMoi.php
|-- www
  |--index.php
j'ai donc crée à dans le dossier library un dossier "Charge" qui est mon dossier test
j'ai crée un class qui s'appelle LoadMoi, voici le scripte
Code :
1
2
3
4
5
6
7
8
9
10
 
<?php
 
class LoadMoi
{
    public function parle()
    {
        echo "salut tu ma demander de charger";
    }
}
mon but je charge la class LoadMoi, et ensuite j'appelle la methode parle();

j'ai donc fait ceci dans mon controller
Code :
1
2
3
4
5
6
7
8
9
10
11
 
class IndexController extends Zend_Controller_Action {
 
    public function init() {
	echo "tete chargement";
        $autoload = Zend_Loader_Autoloader::getInstance();
        $autoload->registerNamespace(Charge_LoadMoi_); //essayer aussi avec Charge_
        $test = new LoadMoi();
        $test->parle();
 
    }
ici cela ne fonctionne pas, j'ai cette erreur:
Code :
1
2
3
 
tete chargement
Fatal error: Class 'LoadMoi' not found in /homez.335/monsite/application/controllers/IndexController.php on line 15
je pensai que si je faisait $autoload->registerNamespace(Charge_LoadMoi),
il va chercher dans mon dossier Charge/LoadMoi.php;

j'ai du oublié quelque chose? ou fait un erreur

merci d'avance de vos réponse
keokaz est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/08/2011, 12h00   #2
Membre chevronné
 
Homme Taoufiq Ben
Développeur Web
Inscription : mai 2009
Messages : 461
Détails du profil
Informations personnelles :
Nom : Homme Taoufiq Ben
Âge : 25
Localisation : Maroc

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : mai 2009
Messages : 461
Points : 645
Points : 645
http://framework.zend.com/manual/fr/...-resource.html
m4riachi est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/08/2011, 12h06   #3
Membre régulier
 
Avatar de Alcide_
 
Homme
Étudiant
Inscription : juin 2008
Messages : 70
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 23
Localisation : France

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : juin 2008
Messages : 70
Points : 78
Points : 78
Ta classe doit s'appeler "Charge_LoadMoi" et non "LoadMoi".
Et si tu veux charger des plugins :
Tu devrais soit ajouter une méthode dans ton bootstrap avec un truc du style :
Code :
1
2
3
4
5
6
7
8
 
/*soit :*/
    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
        'basePath'  => 'Charge/',
        'namespace' => 'Charge',
    ));
/* ou :*/
$loader = new Zend_Loader_PluginLoader(array('Charge_' => 'Charge/'));
Sinon, tu peux aussi ajouter ton namespace dans ton application.ini :
Code :
autoloadernamespaces[] = "Charge_"
Alcide_ est déconnecté   Envoyer un message privé Réponse avec citation 10
Vieux 24/08/2011, 14h50   #4
Membre régulier
 
Inscription : août 2008
Messages : 402
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 402
Points : 79
Points : 79
merci aclide

j'ai donc essayer et mis ce boostrap

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
<?php
 
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
 
    public function init()
    {
    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
        'basePath'  => 'Charge/',
        'namespace' => 'Charge',
    ));
    }
}
je ne savais pas quoi mettre en mehtode j'ai mis funtion init() ?

voic la class loadmoi
Code :
1
2
3
4
5
6
7
8
9
 
<?php
Class Charge_LoadMoi
{
    public function parle()
    {
        echo "chargez moi?";
    }
}
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
<?php
 
class IndexController extends Zend_Controller_Action
{
 
    public function init()
    {
        $resourceLoader = new Charge_LoadMoi();
        $resourceLoader->parle();
    }
 
    public function indexAction()
    {
        // action body
    }
 
 
}
il ne trouve pas la class "Charge_LoadMoi"

lorsque on a cette ligne
Code :
1
2
 
        'basePath'  => 'Charge/',
par t'il au niveau du fichier boostrap ? ou il est réglé a cherche depuis le library ?

dans l'exemple donner par la doc
Code :
1
2
3
4
5
6
7
8
 
    path/to/some/directory/
        acls/
            Site.php
        forms/
            Login.php
        models/
            User.php
le boostrap.php est placé au niveau de "path" ??


par contre
Code :
1
2
 
autoloadernamespaces[] = "Charge_"
cela fonctionne très bien
keokaz est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/08/2011, 15h12   #5
Membre régulier
 
Avatar de Alcide_
 
Homme
Étudiant
Inscription : juin 2008
Messages : 70
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 23
Localisation : France

Informations professionnelles :
Activité : Étudiant
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : juin 2008
Messages : 70
Points : 78
Points : 78
Dans ton bootstrap, si tu veux que tes méthodes soit appelées directement, leur nom doit obligatoirement commencer par le préfixe "_init".
exemple :
Code :
1
2
3
 
protected function _initLoader(){}
protected function _initBdd(){}
Alcide_ est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/08/2011, 16h25   #6
Membre régulier
 
Inscription : août 2008
Messages : 402
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 402
Points : 79
Points : 79
je procède comme ceci?

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
 
<?php
 
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
 
    protected function _initLoader() {
 
        $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
                    'basePath' => 'Charge/',
                    'namespace' => 'Charge',
                ));
    }
}
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
<?php
 
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
        $resourceLoader = new Charge_LoadMoi();
        $resourceLoader->parle();
    }
 
    public function indexAction()
    {
        // action body
    }
}
j'ai cette erreur
Code :
1
2
 
Fatal error: Class 'Charge_LoadMoi' not found in /var/www/clean/application/controllers/IndexController.php on line 7
je remets mes arbres
Code :
1
2
3
4
5
6
7
8
9
10
11
 
projet clean
 
--application
  |--bootstrap
  |-- controlller
  |--IndexController.php
 
--library
  |--Charge
  |--LoadMoi.php
keokaz est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/08/2011, 17h02   #7
Rédacteur/Modérateur
 
Avatar de MaitrePylos
 
Homme Gérard Ernaelsten
DBA & Dev PHP
Inscription : juin 2005
Messages : 3 176
Détails du profil
Informations personnelles :
Nom : Homme Gérard Ernaelsten
Âge : 39
Localisation : Belgique

Informations professionnelles :
Activité : DBA & Dev PHP
Secteur : Service public

Informations forums :
Inscription : juin 2005
Messages : 3 176
Points : 6 463
Points : 6 463
Ton fichier n'est pas dans le répertoire /library/charge
MaitrePylos est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/08/2011, 18h01   #8
Membre régulier
 
Inscription : août 2008
Messages : 402
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 402
Points : 79
Points : 79
merci de ta réponse MaitrePytos

Code :
1
2
 
Ton fichier n'est pas dans le répertoire /library/charge
dans mon dossier library il y a bien un dossier qui s'appelle
Charge,

et un fichier qui s'appelle
LoadMoi.php


avec la methode
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
 
par contre
Code :
Sélectionner tout - Visualiser dans une fenêtre à part
 
1
2
 
 
 
autoloadernamespaces[] = "Charge_"
 
cela fonctionne très bien
cela fonctionne donc je suppose que le fichier est les dossiers sont à leur place ?

j'ai essayer autre chose revoici le boostrap
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
<?php
 
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
 
    protected function _initLoader() {
 
       $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
                 'basePath' => 'Charge/',
                   'namespace' => 'Charge',
                ));
       Zend_Debug::dump($ressourceLoader);
    }
}
là c'est curieux le résultat est NULL

Code :
1
2
3
4
5
6
 
Notice: Undefined variable: ressourceLoader in /var/www/clean/application/Bootstrap.php on line 11
 
NULL
 
Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller class ("Default_ErrorController")' in /usr/share/php/libzend-framework-php/Zend/Controller/Dispatcher/Standard.php:352 Stack trace: #0 /usr/share/php/libzend-framework-php/Zend/Controller/Dispatcher/Standard.php(262): Zend_Controller_Dispatcher_Standard->loadClass('ErrorController') #1 /usr/share/php/libzend-framework-php/Zend/Controller/Front.php(954): Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http)) #2 /usr/share/php/libzend-framework-php/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch() #3 /usr/share/php/libzend-framework-php/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run() #4 /var/www/clean/public/index.php(26): Zend_Application->run() #5 {main} thrown in /usr/share/php/libzend-framework-php/Zend/Controller/Dispatcher/Standard.php on line 352
qu'est ce que j'ai bien pu faire comme betise
keokaz est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/08/2011, 18h24   #9
Modérateur
 
Homme Loïc
Développeur Web
Inscription : février 2011
Messages : 678
Détails du profil
Informations personnelles :
Nom : Homme Loïc
Âge : 26
Localisation : France, Hérault (Languedoc Roussillon)

Informations professionnelles :
Activité : Développeur Web
Secteur : High Tech - Multimédia et Internet

Informations forums :
Inscription : février 2011
Messages : 678
Points : 1 041
Points : 1 041
Tu ecris ressources avec un ss et un autre avec 2
5h4rk est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 24/08/2011, 21h54   #10
Membre régulier
 
Inscription : août 2008
Messages : 402
Détails du profil
Informations forums :
Inscription : août 2008
Messages : 402
Points : 79
Points : 79
merci 5h4rk , décidément j'écrit vraiment n'importe comment

j'ai donc corriger mon boostrap
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
 
<?php
 
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
 
    protected function _initLoader() {
  $Loader = new Zend_Loader_Autoloader_Resource(array(
        'basePath'  => 'Charge/',
        'namespace' => 'Charge',
    )); 
	Zend_Debug::dump($Loader);
    }
}
ce qui donne
Code :
1
2
3
4
5
6
7
8
9
10
 
object(Zend_Loader_Autoloader_Resource)#6 (5) {
  ["_basePath":protected] => string(7) "Charge/"
  ["_components":protected] => array(0) {
  }
  ["_defaultResourceType":protected] => NULL
  ["_namespace":protected] => string(6) "Charge"
  ["_resourceTypes":protected] => array(0) {
  }
}
j'ai réessayer de charger avec le controlleur
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
<?php
class IndexController extends Zend_Controller_Action
{
    public function init()
    {
	echo "<h1>chargement</h1>";
       $loader = new Charge_LoadMoi();
        $loader->parle();
    }
    public function indexAction()
    {     
    }
}

Code :
1
2
3
 
chargement
Fatal error: Class 'Charge_LoadMoi' not found in /var/www/2module/application/controllers/IndexController.php on line 7
pour retesté si je n'ai pas fait de bêtise j'ai décommenter la ligne du chargement
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
[production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 1
;autoloadernamespaces[] = "Charge_" // si je décommennte la methode parle fonctionne
[staging : production]
 
[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
 
 
[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
keokaz est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 16h42.


 
 
 
 
Partenaires

Hébergement Web