Get form and Twig

Hi All,
I’m trying to get a url like “www.somedomail.com/tasks/search/searchtext” the only problem is making a form that would make it look like that with twig any ideas? I’ve notices that the path_for() twig function won’t work with get either…

If your problem is with path_for(), make sure that you are setting a name for the route with setName() like :

$app->get('/tasks/search/searchtext', $routeCallback)->setName('formRoute');

then on twig you can use:

<form action="{{ path_for('formRoute') }} method="get">

P.S: if you form method is post make sure to add a route with $app->post(... instead of $app->get(...

Apparently you need to set the parameters with path_for() so just passing the name won’t cut it… “Message: An exception has been thrown during the rendering of a template (“Missing data for URL segment: keywords”) in “tasks.html” at line 50.”

I have this at the moment:

$app->get('/tasks/search/{keywords}', function ($request, $response, $args) {
    // some code
})->setName("tasks.search");

and in twig I have:

<form class="row" action="{{ path_for('tasks.search') }}" method="get">
    <input class="" name="/"  type="text" placeholder="Search" />
</form>

With the previous error stated… Although if I try this:

<form class="row" action="tasks/search" method="get">
    <input class="" name="/"  type="text" placeholder="Search" />
</form>

I get a url like this which is annoying “/tasks/search?%2F=somesearch”

So your problem isn’t with path_for(). You cannot do what you want with this method.
You will have to write some javascript to redirect the page to your desired search url when someone submit the form.

Is there a .htaccess way of doing this?

If its a search query just change the path of the route to:

$app->get('/tasks/search', function ($request, $response) {
    $keywords = $request->getArgument('keywords');
})->setName('tasks.search');

then add the following in your form:

<form class="row" action="{{ path_for('tasks.search') }}" method="get">
    <input class="" name="keywords"  type="text" placeholder="Search" />
</form>

And that should generate a url like /tasks/search?keywords=somesearch