Liana.R
Liana.R

Reputation: 124

Get user input data from form in twig file with Symfony 4

I'm trying to create a little search bar on my website with symfony 4. How do I get the the user input data from the form which can be used in the controller. The form looks like this :

 //.../navbar.html.twig

<form action="search" method="get" >
<input type="text" placeholder="Search..">
<button type="submit" class="btn btn-default">Search</button>
</form>

Upvotes: 0

Views: 2473

Answers (1)

Aurelien
Aurelien

Reputation: 1507

Since you don't seem to be using a FormType and your form's method is 'GET':

First your need a name to your input. ex:

<input type="text" name="search" placeholder="Search..">

Then just pass the request service to your action in your controller and get the desired parameter.

public function yourAction(Request $request){
    $searchString = $request->get('search');
}

Edit: I strongly recommend using symfony's form component tho. Doc here : https://symfony.com/doc/current/forms.html

Upvotes: 2

Related Questions