PHP code example of madmatt / silverstripe-funnelback

1. Go to this page and download the library: Download madmatt/silverstripe-funnelback library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

madmatt / silverstripe-funnelback example snippets



namespace App\Controllers;

use Madmatt\Funnelback\SearchService;

class SearchController extends \PageController
{
    private static $dependencies = [
        'searchService' => '%$' . SearchService::class
    ];

    public SearchService $searchService;

    public function index(HTTPRequest $request)
    {
        $keyword = $request->getVar('q');
        $start = $request->getVar('start') ?? 0;

        // If a keyword has been supplied, perform a search and return the results.
        // Otherwise, don't bother performing an empty search.
        if ($keyword) {
            return [
                'Query' => DBField::create_field('Varchar', $keyword)
                'Results' => $this->searchService->search($keyword, $start),
            ];
        } else {
            return [];
        }
    }
}



use SilverStripe\CMS\Controllers\ContentController;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\TextField;

class PageController extends ContentController
{
    public function SearchForm(): Form
    {
        $form = Form::create(
            $this,
            __FUNCTION__,
            FieldList::create([
                TextField::create('q', 'Search query')
            ]),
            FieldList::create([
                FormAction::create('search')
            ])
        );

        $form
            ->setFormAction('/search') // Override the standard form action URL to always be /search
            ->setFormMethod('GET', true) // Ensure the form sends the search query in the URL so it can be bookmarked and cached etc
            ->disableSecurityToken(); // Turn off CSRF protection for this form, it's not