Home > Enterprise >  Errof of arguments in Symfony\Bridge\Twig\Extension\RoutingExtension::getPath()
Errof of arguments in Symfony\Bridge\Twig\Extension\RoutingExtension::getPath()

Time:11-15

In a Controller function, I pass an array to a twig render :

        /**
        * @Route("/core/olympiades,{choix}", name="core_olympiades")
        */
        public function olympiades(Request $request,$choix)
        {

                $repo=$this->getDoctrine()->getRepository(OdpfArticle::class);

                $article=$repo->findOneBy(['choix'=>$choix]);

                $texte=$article->getTexte();

                $tab=[ 'choix'=>$choix, 'texte'=>$texte];

                //dd($tab);

                return $this->render('core/odpf-olympiades.html.twig', $tab);
        }

And the returned exception say :

    Argument 2 passed to Symfony\Bridge\Twig\Extension\RoutingExtension::getPath() must be of the type array, string given

My dd($tab); give

    CoreController.php on line 71:

    array:2 [▼

      "choix" => "c_est_quoi"

      "texte" => """

        <p>C'est un concours scientifique expérimental qui s'adresse à des équipes de deux à six lycéens encadrés par un ou deux professeur(s), en liaison éventuelle av ▶
                                <p>Les Olympiades de Physique France permettent à de petites équipes d'élèves de vivre pendant plusieurs mois une passionnante a ▶
                                <p>S'il est à dominante physique, le concours s’ouvre fréquemment sur les disciplines frontières (chimie, automatique, biologie) ▶
                                <p>Le projet peut prendre place dans le cadre d'un atelier scientifique, dans la préparation du Grand Oral ou dans le prolongeme ▶
                                <p>La finale est suivie d'une exposition publique des projets des élèves qui précède la remise des prix.</p>

        """

    ]

So, I'm sure the array ($tab) passed to the render is an array, not a simple string. In the Entity class OdpfArticle, the 'texte' is described as a 'blob'. The beginning of the twig render is :

    {% extends "base.html.twig" %}

    {% block title %}
        C'est quoi - {{ parent() }}
    {% endblock %}

    {% block stylesheets %}
        {{  parent() }}
        <link rel="stylesheet" href="{{ asset('css/odpf-olympiades.css') }}">
    {% endblock %}

    {% block contenu %}
    <div id="wrapper">
        <div >
        {% include 'core/menu-haut.html.twig' %}
    
        <div >
            <span ><a href="/"></a></span>
            <nav >
                <ul >
                    <li >
                        <a  href="#">Actus</a>
                    </li>
                    <li >
                        <a  href="{{ path('core_olympiades','c_est_quoi') }}">Les Olympiades de Physique France</a>
                    </li>
                    <li >
                        <a  href="#">Le Concours 2021-2022</a>
                    </li>
                    <li >
                        <a  href="#">Revivez les éditions passées</a>
                    </li>
                    <li >
                        <a  href="#">Partenaires</a>
                    </ul>
                    </nav>
                </div>

            <div >
                <div >
                    <h1 >
                        Les Olympiades <br>de Physique France
                    </h1>
                    <div >
                        <div >
                            <div >
                                <nav>
                                <ul >
                                    <li ><a href="/" >C'est quoi ?</a></li>
                                    <li class="nav-item"><a href="#" class="nav-link">Comment ça se passe ?</a></li>
                                    <li class="nav-item"><a href="#" class="nav-link">Pourquoi participer ?</a></li>
                                    <li class="nav-item"><a href="#" class="nav-link">Les aides ?</a></li>
                                    <li class="nav-item"><a href="#" class="nav-link">Les récompenses ?</a></li>
                                    <li class="nav-item"><a href="#" class="nav-link">Qui organise ?</a></li>
                                    <li class="nav-item"><a href="#" class="nav-link">La presse en parle</a></li>
                                    <li class="nav-item"><a href="#" class="nav-link">Les vidéos</a></li>
                                </ul>
                                </nav>
                            </div>
                        </div>
                        {%if choix=='c_est_quoi' %}
                        <div class="col-md-6 col-lg-8">
                            <div class="col-centre">
                                <h2 class="sous-titre">C'est quoi ?</h2>
                                <div class="contenu">
                                    
                                </div>
                            </div>
                                </div>
                {% endif %}
                    </div>
                </div>

If I comment from <div > to the </div>, the error don't appear (and my menu-ppal no more !)

Thinking the CSS could be important, I give here the css of the part in <div> :

    @import url(http://fonts.googleapis.com/css?family=Oxygen:300,400,700);
    a {
        text-decoration: none;
        color:black;
    }
    .container {
        width:100%;
        padding-right: 15px;
        padding-left: 15px;
        margin-right: auto;
        margin-left: auto;
    }
    @media (min-width: 576px){
        .container {
            max-width: 540px;
        }
    }
    @media (min-width: 786px){
        .container {
            max-width: 720px;
        }
    }
    @media (min-width: 992px){
        .container {
            max-width: 960px;
        }
    }
    @media (min-width: 1200px){
        .container {
            max-width: 1140px;
        }
    }
    @media screen and (min-width: 751px) {
        .ppal-pages {
            position: absolute !important;
            padding: 0;
            z-index: 3;
        }
        .ppal-pages span.site-logo {
            background: url('../odpf-images/site-logo-285x75.png') #fff;
            width: 285px;
            height: 78px;
            position: relative;
            top: -16px;
            display: block;
        }
    }
    @media screen and (max-width: 750px) {
        .ppal-pages {
            z-index: 0;
        }
        .ppal-pages span.site-logo {
            background: url('../odpf-images/site-logo-285x75.png') #fff;
            width: 285px;
            height: 78px;
            position: relative;
            display: block;
        }
    }
    .menu-ppal {
        display: inline;
        line-height: 1;
        box-sizing: content-box;
    }
    .menu-ppal li {
        display: block;
        padding: 0 15px 0 0;
        box-sizing: content-box;
    }
    .menu-ppal-text a {
        display: inline;
        text-decoration: none;
        margin: 0;
        padding: 0;
        font-family: 'Oxygen', sans-serif;
        font-size: 16px;
        font-weight: 700;
        background: #d6dbdf;
        color: #111a21;
        text-align: center;
        text-transform: uppercase;
    }
    .menu-ppal-text a:active {
        background: #b1191d;
        color: #ffffff;
        cursor: default;
    }

    .menu-ppal-text a:hover {
        background: #111a21;
        color: #ffffff;
    }
    .menu-ppal-text li:last-child a {
        margin: 0;
    }
    .menu-ppal-text .active a,
    .menu-ppal-text .active a:hover {
        background: #b1191d;
        color: #ffffff;
        cursor: default;
    }

CodePudding user response:

You must replace this

{{ path('core_olympiades','c_est_quoi') }}

by this

{{ path('core_olympiades', {'choix': 'c_est_quoi'}) }}

For more information, this is an example in the Routing documentation: https://symfony.com/doc/current/routing.html#generating-urls-in-javascript

  • Related