Adding search to a site (Beginner)

I’m following this page to add search to a site, but I’m completely confused.

I’ve created the site/templates/search.php
I’ve create the site/controllers/search.php

…as shown on that page, however…

How do I actually do a search?
Where is the search filed shown?
Do I have to create a .yml file for search page?

Right now I’m not doing anything special, just following that page.

When I do manually create a empty dummy content/search/search.txt file, I get just a blank white empty page.

I’m obviously missing something, but not sure what my next turn is. I’d love some help with this, or at least a point in the right direction.

Cheers.

P.S. the site is on my test domain: kamgar.net

The search field is in the form in the template: Search | Kirby CMS

So when you open the search page, you should see the form. Make sure you have no typos in your file names anywhere.

So
/content/search/search.txt
/site/templates/search.php
/site/controllers.search.php

should do the job.

Thanks for getting back to me.

I have all of those pages pages as I explained, and all I see is a blank page.

This is what I have, again direct copy from that page.

/content/search/search.txt

(is blank)

/site/templates/search.php

<form>
	<input type="search" aria-label="Search" name="q" value="<?= html($query) ?>">
	<input type="submit" value="Search">
</form>

<ul>
	<?php foreach ($results as $result): ?>
	<li>
		<a href="<?= $result->url() ?>">
			<?= $result->title() ?>
		</a>
	</li>
	<?php endforeach ?>
</ul>

/site/controllers/search.php

<?php

return function ($site) {

	$query   = get('q');
	$results = page('blog')->search($query, 'title|text');
	$results = $results->paginate(20);

	return [
		'query'      => $query,
		'results'    => $results,
		'pagination' => $results->pagination()
	];

};

In the example, we search a page called blog, that only makes sense if you have a blog page, you probably want to search another page or even the site

<?php

return function ($site) {

	$query   = get('q');
	$results = $site->search($query, 'title|text'); // use site object to search
	$results = $results->paginate(20);

	return [
		'query'      => $query,
		'results'    => $results,
		'pagination' => $results->pagination()
	];

};

If you have debug disabled, you won’t see the error message, for cases like that, enabling debug in config would be the first step.

Yes, I do have a /blog page but now copied your last piece of code.

Good point about the debug! I’ve now enabled the debug, and getting this…

But $query is defined in the controller, so should not be undefined in the template.

What happens if get() doesnt find 'q'? Does it return null then? That doesnt solve the undefined issue though does it.

If get('q') is null, then the value of $query is null, but the variable is still defined.

(On a side note, I know that the recipe works…)

If the recipe works, then what could be the issue here? Bearing in mind that I’ve copied the code from the cookbook as is.

I’m completely confused :expressionless:

No idea, is the project on GitHub?

No GitHub, as I’m not a dev so no need to use it. But happy to DM you the FTP details for the test server.

DM sent :slight_smile:

Cheers!

Thanks @texnixe for finding the issue!

It was a typo I made for the controller file :expressionless:

Everything now works as needed.

I feel silly, but we live and learn.