Home > Blockchain >  How to pass a parameter in php route
How to pass a parameter in php route

Time:04-04

I have a problem to pass a parameter into a route in html, I got this message :

ErreurAn exception has been thrown during the rendering of a template ("Parameter "id" for route "getuser" must match "[^/] " ("" given) to generate a corresponding URL.").

Actually I'm trying to edit a row in a datatable js, so first step is to get the id by user.

-> I Have a Controller to get a user by id :

`$controllers->get('/getuser/{id}', function (App $app,Request $request) {

    $user  =$app->service('User')->getUser($request->get('id'));   
     return $app->render('listAllUsers.twig', ['user' =>$user]); 
        
    })->bind('getuser')->before($this['loginok']);`  

-> And the problem is in the twig, I cannot pass the parameter 'id' :

<form id="form3" action="{{ app.route('getuser',{'id':user.id})}}" method="get">

I know it's a matter of syntax, I should add a '/' but I dont know where.

Can someone help me with this?

I tried this :

`action="{{ app.route('getuser/',{'id': user.id})}}"

And this :

`<form id="form3" action="{{ app.route('getuser/{id}',{'id': user.id})}}" method="get">``

CodePudding user response:

Here is the complete code :

$(function () {

 var table=$('#user');
    var dtable;
                                        
    dtable= table.DataTable({
                                             
    language : {
      url:'{{app.path.datatables.fr}}'
               },
                                       
      dom: 'Blfrtip',
      select: true,
      lengthMenu: [
      [ 10, 15, -1 ],
      [ '10', '15', 'All' ]
     ],
             
      buttons: 
      [
       {
       extend: 'print',
       text:'<i ></i>',
       titleAttr: 'Imprimer',
       messageTop: 'Liste des Clients Privés'  
       },    
       {
       extend:    'pdfHtml5',
       text:      '<i ></i>',
       titleAttr: 'PDF',
       messageTop: 'Liste des Clients Privés'
       },   
      {
       extend:    'excelHtml5',
       text:      '<i ></i>',
       titleAttr: 'Excel',
       messageTop: 'Liste des Clients Privés'
       },
      {
       extend:    'csvHtml5',
       text:      '<i ></i>',
       titleAttr: 'CSV',
       messageTop: 'Liste des Clients Privés'
       }
       
       ],
        
     columns:
     [ 
     {'title':'Id'},
     {'title':'Nom'},
     {'title':'Prénom'},
     {'title':'Adresse'},
     {'title':'Login'},
     {'title':'Accès'},
     {'title':'Comptes client'}
     ]
     });    /* Fin Datatable */
     
      /* MODAL Modifier client Privé */
       $('.btMod').click (function(e){
       e.preventDefault();
       $('#idClient').val($(this).data('id'));
       $('#formMod').modal({
       show:true,
       backdrop:'static'
       });
       });
       
       
       /* MODAL BOUTON VALIDER FORMULAIRE MODIFIER CLIENT PRIVE */
       $('#validerMod').click(function(e){
       e.preventDefault();
       $.ajax({
        url:$('#form3').attr('action'),
        type: $('#form3').attr('method'),
        data: $('#form3').serialize()
                                                                                                             
        }).done(function(data){
        $('#formMod').modal('hide');//cacher fenetre
  
        window.location.href='{{app.route('clientsPrive')}}';
        alert("Client modifié avec succès !");                             
                                     
        }).fail(function(data){
        console.log(data.responseText);
        });
        });
});
        
 <div  style="  display:flex;  margin-left: auto; margin-right:auto; width:100%; margin-bottom: 5vw; margin-top:1vw;
             border:1px solid #b1e7e7; background:rgba(102, 204, 255, 0.05);"> 
            <div  > 
       
               <table id="user"  > 
                  
                    {%if app.isAdmin %}     
                 <button id="bt-{{user.id}}" data-id="{{user.id}}"  style="background:#0088cc; font-size:0.4vw;" title="Nouveau client">
                 <span class ="glyphicon glyphicon-plus" style="font-size:1vw;"></span></button> 
                 {%endif%} 
                   
                <!-- TABLE USER -->  
                {% for user in users %}  
                <tr> 
                    <td> {{user.id}} </td> 
                    <td> {{user.nom}} </td>                  
                    <td> {{user.prenom}} </td> 
                    <td> {{user.adresse}} </td> 
                    <td> {{user.login}} </td>                     
                    <td> {{user.AccesString}} </td>
                      
                    <!--BOUTONS DANS DATATABLE-->  
                    <td>
                       {%if app.isAdmin %}    
                           
                            <!-- Bouton Ajouter compte client-->
                           <button id="bt-{{user.id}}" data-id="{{user.id}}"  style="background:rgba(0, 136, 204, 0.5); font-size:0.4vw;" title="Nouveau compte">
                           <span class ="glyphicon glyphicon-plus" style="font-size:1vw;"></span></button> 
                           
                           <!-- Bouton Modif client-->
                           <button id="bt-{{user.id}}" data-id="{{user.id}}"  style="background:rgba(0, 136, 204, 0.5); font-size:0.4vw;" title="Nouveau compte">
                           <span class ="fa fa-pencil" style="font-size:1vw;"></span></button> 
                             
                           <!-- Bouton Liste des comptes par client-->
                             <a style="font-size:0.4vw; background:rgba(0, 136, 204, 0.7);" href="{{app.route('listeCp',{id:user.id})}}"  title="Liste des comptes">
                             <span class ="glyphicon glyphicon-th-list" style="font-size:1vw;"></span></a>
                             
                             {%endif%}
                     </td>
                </tr>
                {% endfor%}
        </table>
        </div>    
                </div>
    
    
  <!--POP UP MODIF CLIENT-->
          
         <div  tabindex="-1" role="dialog" id="formMod">
                   
  <div  role="document">
    <div >
      <div >
        <button type="button"  data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 >Modifier Client</h4>
      </div>
      
        
        <!--FORMULAIRE -->
        <div >
          
            <!-- APPEL FONCTION -->
             <form  id="form3" action="{{ app.route('getuser',{'id':user.id})}}" method="get">
                               
                <input type ="hidden" name="id" value="" id="idClient">
                
                <div >
                    <label for="nom" > Nom</label>
                    <div >
                        <input type ="text"  id="nom" name="nom" placeholder="Nom " value="{{user.nom}}">
                    </div></div>
                    
                <div >
                    <label for ="prenom" > Prénom</label>
                    <div >
                        <input type ="text"  id="prenom" name="prenom" placeholder="Prénom" value="{{user.prenom}}">
                    </div></div>
                    
                <div >
                    <label for ="adresse" > Adresse</label>
                    <div >
                        <input type ="text"  id="adresse" name="adresse" placeholder="Adresse" value="{{user.adresse}}">
                    </div></div>
                    
                    
                     <div >
                    <label for ="login" > Login</label>
                    <div >
                        <input type ="text"  id="login" name="login" placeholder="Login" value="{{user.login}}">
                    </div></div>
                    
                       <div >
                    <label for ="password" > Mot de passe</label>
                    <div >
                        <input type ="password"  id="password" name="password" placeholder="Mot de passe" value="{{user.password}}">
                    </div></div> 
                    
                     <!-- archive caché-->    
                <input type ="hidden"  id="archive" name="archive" placeholder="archive" value="{{user.archive}}">
                   
                 <!-- acces caché-->    
                <div  >        
                    <div >
                       <select   id="acces" name="acces">
                           <option value="2">Client Prive </option>
                           <option value="3">Client Pro </option>
                            <option value="1">Admin </option>
                       </select>
                    </div>
                  </div>
            </form>
      </div>
        
         <!--BOUTONS FERMER ET VALIDER DANS FORMULAIRE MODIFIER CLIENT-->
      <div >
        <button type="button"  data-dismiss="modal">Fermer</button>
        <button type="button"  id="validerMod">Valider</button>
      </div>
    </div>
  </div>
</div>

CodePudding user response:

In Laravel, which is based on Symfony, you would pass parameters in named routes like this

<form 
  method="GET" 
  action="{{ app.route('getuser',['id' => $id ])}}"
>
    
    ...
</form>

CodePudding user response:

I also tried this :

 <form  id="form3" action="{{ app.route('getuser',{'id':2})}}" method="get">

This time no errors in the browser , but nothing happens when I click the button, I should be able to see the id user n°2, but no.

And this :

 <form  id="form3" action="{{ app.route('getuser',{'id':user(2)})}}" method="get">

Here I have an error : unknown 'user' fonction

  • Related