IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Symfony PHP Discussion :

Warning: strpos() expects parameter 1 to be string, array given in [2.x]


Sujet :

Symfony PHP

  1. #1
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

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

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut Warning: strpos() expects parameter 1 to be string, array given in
    Bonjour
    j'essaye d'afficher mes images uploadée via cette template Twig:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    <img src="{{ asset(entity.getWebPath("$arrPaths['file1']")) }}" />
    <img src="{{ asset(entity.getWebPath("$arrPaths['file2']")) }}" />
    <img src="{{ asset(entity.getWebPath("$arrPaths['file3']")) }}" />
    <img src="{{ asset(entity.getWebPath("$arrPaths['file4']")) }}" />
    Et cela depuis ce modèle :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public function getWebPath()
        {
    	$arrPaths = array();
            $arrPaths['file1'] = null === $this->file1 ? null : $this->getUploadDir(). $this->file1;
            $arrPaths['file2'] = null === $this->file2 ? null : $this->getUploadDir(). $this->file2;
            $arrPaths['file3'] = null === $this->file3 ? null : $this->getUploadDir(). $this->file3;
            $arrPaths['file4'] = null === $this->file4 ? null : $this->getUploadDir(). $this->file4;
     
             return $arrPaths;
        }
    Par la suite j'ai cette erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    An exception has been thrown during the rendering of a template ("Warning: strpos() expects parameter 1 to be string, array given in /Symfony/vendor/symfony/src/Symfony/Component/Templating/Asset/PathPackage.php line 47") in ::base.html.twig at line 10.
    Quelqu'un peut m'aider a débugger ce soucis.
    En vous remerciant d'avance.

  2. #2
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    y'a pas de $arrPaths dans les vues twig

  3. #3
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

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

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut
    Oui je confus entre les Template Twig et PHP !! en effet les tableaux en Twig :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    {{ array.bar }}
    {{ array['bar'] }}
    J'ai fait ceci :


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    <img src="{{ asset(entity.getWebPath({{ arrPaths.file1 }})) }}" />
    <img src="{{ asset(entity.getWebPath({{ arrPaths.file2 }})) }}" />
    <img src="{{ asset(entity.getWebPath({{ arrPaths.file3 }})) }}" />
    <img src="{{ asset(entity.getWebPath({{ arrPaths.file4 }})) }}" />

    ya une erreur : bete :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{"

  4. #4
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    juste

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    {{ asset(entity.getWebPath(arrPaths.file1)) }}
    ou

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    {{ asset(entity.getWebPath(arrPaths['file1'])) }}

  5. #5
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

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

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut
    Merci eh bien
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Variable "arrPaths" does not exist in
    dans ma vue!! un autre soucis !! faut que je le définis dans mon controlleur ?

  6. #6
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    oui si tu la passe pas à ta vue c'est normale qu'elle n'existe pas

  7. #7
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

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

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut
    Je vien de le faire que maintenant !!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $arrPaths = array();
            $arrPaths['file1'] = 'file1';
            $arrPaths['file2'] = 'file2';
            $arrPaths['file3'] = 'file3';
            $arrPaths['file4'] = 'file4';
     
     
            return $this->render('MyBundle:Entity:index.html.twig', array(
                'entities' => $entities,
                'arrPaths'    => $arrPaths,
      ));
    j'ai eu un soucis : ca revient a la 1ere erreur :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    An exception has been thrown during the rendering of a template ("Warning: strpos() expects parameter 1 to be string, array given in Symfony/vendor/symfony/src/Symfony/Component/Templating/Asset/PathPackage.php line 47") in ::base.html.twig at line 10.
    Je pense qu'il faut faire avec template PHP non pas en Twig ??

  8. #8
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    que retourne getWebPath

  9. #9
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

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

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut
    Citation Envoyé par stealth35 Voir le message
    que retourne getWebPath
    return $arrPaths;

  10. #10
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    Citation Envoyé par aitiahcene Voir le message
    return $arrPaths;
    pourquoi passer un paramètre a une méthode get, tu vois bien que ça colle pas

  11. #11
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

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

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut
    Certainement !! donc a ton avis la déclaration du tableau arrPaths dans le controlleur est fausse ?? faut définir la fonction getWebPath dedans

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $arrPaths = array();
            $arrPaths['file1'] = 'file1';
            $arrPaths['file2'] = 'file2';
            $arrPaths['file3'] = 'file3';
            $arrPaths['file4'] = 'file4';
     
     
            return $this->render('MyBundle:Entity:index.html.twig', array(
                'entities' => $entities,
                'arrPaths'    => $arrPaths,
      ));

  12. #12
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    montre ta méthode getWebPath

  13. #13
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

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

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut
    Citation Envoyé par stealth35 Voir le message
    montre ta méthode getWebPath
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public function getWebPath()
        {
    	$arrPaths = array();
            $arrPaths['file1'] = null === $this->file1 ? null : $this->getUploadDir(). $this->file1;
            $arrPaths['file2'] = null === $this->file2 ? null : $this->getUploadDir(). $this->file2;
            $arrPaths['file3'] = null === $this->file3 ? null : $this->getUploadDir(). $this->file3;
            $arrPaths['file4'] = null === $this->file4 ? null : $this->getUploadDir(). $this->file4;
     
             return $arrPaths;
        }
    et getUploadDir() retourne un dossier la ou les images vont être stockés !!

  14. #14
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    donc

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    <img src="{{ asset(entity.getWebPath.file1) }}" />
    <img src="{{ asset(entity.getWebPath.file2) }}" />
    <img src="{{ asset(entity.getWebPath.file3) }}" />
    <img src="{{ asset(entity.getWebPath.file4) }}" />
    mais les fichiers devrai être dans une autre table

  15. #15
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

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

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut
    Non, comme c'est je fais :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    <img src="{{ asset(entity.file1) }}" />
    <img src="{{ asset(entity.file2) }}" />
    <img src="{{ asset(entity.file3) }}" />
    <img src="{{ asset(entity.file4) }}" />
    Les images ne s'affichent pas, icone vide !!!

  16. #16
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    fais comme je dis, et puis regarde dans ta source HTML et tu comprendras

  17. #17
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

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

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut
    Les images ne s'affichent pas j'ai tout essayer !! ton code est équivalent :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <img src="{{ asset(entity.getWebPath().file1) }}" />
    <img src="{{ asset(entity.getWebPath().file2) }}" />
    <img src="{{ asset(entity.getWebPath().file3) }}" />
    <img src="{{ asset(entity.getWebPath().file4) }}" />
    -------------
    <img src="{{ asset(entity.file1) }}" />
    <img src="{{ asset(entity.file2) }}" />
    <img src="{{ asset(entity.file3) }}" />
    <img src="{{ asset(entity.file4) }}" />
    la sources est juste les images sont dans le dossier définis dans la méthode getUploadDir() !!!

  18. #18
    Expert éminent sénior

    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    7 920
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 7 920
    Points : 10 726
    Points
    10 726
    Par défaut
    montre la source HTML

  19. #19
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

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

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut
    eh bien finalement les images sont enregistrer dans Symfony/web/ au lieu du Symfony/web/Test, la source n'est pas juste

    je te montre tous les methodes de l'entité

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    public function getWebPath()
        {
    	$arrPaths = array();
            $arrPaths['file1'] = null === $this->file1 ? null : $this->getUploadDir(). $this->file1;
            $arrPaths['file2'] = null === $this->file2 ? null : $this->getUploadDir(). $this->file2;
            $arrPaths['file3'] = null === $this->file3 ? null : $this->getUploadDir(). $this->file3;
            $arrPaths['file4'] = null === $this->file4 ? null : $this->getUploadDir(). $this->file4;
     
     
             return $arrPaths;
     
     
        }
     
        protected function getUploadRootDir()
        {
     
            return __DIR__.'/../../../../web/'.$this->getUploadDir();
        }
     
        protected function getUploadDir()
        {
     
            return 'Test';
        }
     
        public function uploadFile1()
        {
     
            $this->file1->move($this->getUploadRootDir(), $this->file1->getClientOriginalName());
     
     
            $this->file1 = $this->file1->getClientOriginalName();
     
     
        }
    	public function uploadFile2()
        {
     
            $this->file2->move($this->getUploadRootDir(), $this->file2->getClientOriginalName());
     
     
            $this->file2 = $this->file2->getClientOriginalName();
     
     
        }
    	public function uploadFile3()
        {
     
            $this->file3->move($this->getUploadRootDir(), $this->file3->getClientOriginalName());
     
     
            $this->file3 = $this->file3->getClientOriginalName();
     
     
        }
    	public function uploadFile4()
        {
     
            $this->file4->move($this->getUploadRootDir(), $this->file4->getClientOriginalName());
     
     
            $this->file4 = $this->file4->getClientOriginalName();
     
     
        }
    dans mon code HTML les images se trouve dans :un exemple
    Symfony/web/Testpicture.jpg
    au lieu de Symfony/web/Test/picture.jpg y a un / qui manque !!!?!!!!!

  20. #20
    Membre averti
    Homme Profil pro
    Développeur Web
    Inscrit en
    Avril 2012
    Messages
    394
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Hauts de Seine (Île de France)

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

    Informations forums :
    Inscription : Avril 2012
    Messages : 394
    Points : 347
    Points
    347
    Par défaut
    eh bien c'était a cause de la methode : getUploadDir() il fallait ajouter un "/" à Test:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    protected function getUploadDir()
        {
     
            return 'Test/';
        }
    Merci !!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [MySQL] Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in
    Par Trebor_ dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 29/06/2015, 14h10
  2. [SimpleXML] Warning:simplexml_load_string()expects parameter 1 to be string
    Par Invité dans le forum Bibliothèques et frameworks
    Réponses: 3
    Dernier message: 10/06/2015, 18h07
  3. Réponses: 15
    Dernier message: 03/02/2015, 16h14
  4. [MySQL] Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in
    Par Sarah sh dans le forum PHP & Base de données
    Réponses: 3
    Dernier message: 08/12/2013, 18h12
  5. Réponses: 5
    Dernier message: 14/12/2011, 07h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo