Decoupling Form Submission from Route Parameters in Laravel
When building a search interface, it is common to want a single URL that accepts query arguments like term and category. However, a frequent stumbling block occurs when mixing named route parameters directly into the form's action attribute. In your specific case, the form submits to /search/{category}/{term}, but the browser ignores the placeholders in the URL and sends them as standard query parameters instead. This results in a redirect to search/%7Bcategory%7D/%7Bterm%7D?term=asdasd&category=auto. The double encoding of the brackets (%7B) indicates that the browser is treating the route definition string literally rather than resolving it against your actual controller logic.
The core issue lies in how Form::open resolves the route helper. When you define a route using named parameters like {category} and {term}, Laravel treats these as variables to be filled by the routing engine upon a direct GET request. However, if you attempt to pass those same variable names inside the form's action or rely on the route name while expecting the browser to populate them via URL rewriting, you run into a conflict. The browser does not know that /search/{category}/{term} should be interpreted as a template; it sees it as a static path containing special characters.
To solve this without JavaScript, you must separate the definition of your route parameters from the method used to trigger the form submission. The cleanest approach involves defining your route with only named parameters and ensuring the form targets that specific route name or URL while allowing Laravel to handle the parameter binding.
First, adjust your route definition to accept the parameters in a logical order that matches how you intend to access them in the controller, or simply use query strings if the URL structure doesn't require strict positional segments. If you strictly need the URL to look like /search/auto/asdasd, you must ensure the form action points exactly to that resolved path. However, often the best practice for search forms is to let the route handle the parameters as query variables rather than URI segments, unless there is a specific SEO requirement for them to be part of the path.
Consider modifying your route to accept query parameters instead of path parameters if you want to keep the URL simple and standard:
Route::get('/search', ['as' => 'search', 'uses' => 'SearchController@search'])
->where('term', '.*')
->where('category', '[a-z]+');
With this configuration, your form can simply submit to the named route search, and Laravel will automatically bind the incoming query string parameters (?term=...&category=...) to the controller method arguments. You can then access them via $request->input('term') or type-hinted properties like public function search(Request $request).
If you absolutely require the parameters to be part of the URL path (e.g., /search/auto/asdasd), the form action must match that exact path. You cannot pass {category} and {term} as literal strings inside the form tag; they must be resolved by the routing system before the browser makes the request. To achieve this without JavaScript, ensure your route is defined with a unique name and use that name in the form:
Route::get('/search/{category}/{term}', ['as' => 'search', 'uses' => 'SearchController@search']);
Then, update your Blade form to explicitly target the named route. The key here is that the Form::open helper will generate a URL using the route's name. If the browser sees /search/{category}/{term} in the generated HTML, it means the route was not resolved correctly during compilation, or the uses closure is expecting arguments that don't match the incoming request signature.
In your original code, the form generates an action like /search/{category}/{term} which the browser attempts to submit as-is. If the server expects these to be query parameters but receives them as path segments (or vice versa), you see the double-encoded brackets. The solution is often to simplify the route signature to match the form submission method. If your controller method accepts arguments, ensure they align with how the data arrivesâeither as URI segments or query strings.
For instance, if you change the route to accept query parameters by default (no path variables) and update the controller to read from $request->query(), the form will submit cleanly to /search?term=...&category=.... This is usually the "Laravel way" for search functionality unless a specific URL structure is mandated by SEO strategy.
By aligning your route definition with the expected input method and leveraging Laravel's named routes, you eliminate the need for manual URL construction or JavaScript redirection. The framework handles the mapping of incoming request data to your controller arguments seamlessly, provided the route signature and form action are consistent.