0

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>

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

using the link @Aurelien suggested is really good it will save you alot of time later on and will help you understand symfony much better.
SearchType Field. This renders an <input type="search" /> field, which is a text box with special functionality supported by some browsers. : symfony.com/doc/current/reference/forms/types/search.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.