Structuring site search

Right now I’m trying to set up a sitewide search field that appears in the header on every page. It was easy to set up the functionality for a dedicated search page using Bastian’s tutorial but I’m not sure how to display the search bar in the header snippet and keep the logic separate in the search.php controller. Is there any way to ‘link’ a template or snippet to a differently-named controller?

How I’d do it:

<form action="<?php echo page('search')->url() ?>" method="post">
  <input type="search" name="q"><input type="submit" value="Search">

Keep that search form in the header snippet and pass it on submit to the search.php - there you can do pretty much the stuff from the tutorial, including the search.phpcontroller which works this way.

Just make sure that you either submit the form via the GET method – or if you use POST you need to adapt the controller to the following, I guess:

$query   = post('q');

Hope that helps for the next steps :wink:


Changing get('q') to post('q') throws an error, but leaving it as-is and removing method="post" seems to work perfectly!

Ha, just learned that get() fetches GET and POST parameter. So no matter which method you choose, stick with get('p') :wink:

1 Like