Result Landing Page (SRLP) URL
Change Default SRLP URL
The Search Result Landing Page (SRLP) URL in Magento (without any changes) is /catalogsearch/result.
In v2.x/v3.x version of the Klevu module an extra SRLP route was added with a URL of /search. Having 2 routes is not ideal. In v4.x there is only one search route and by default, that route is the same as the Magento default, i.e. /catalogsearch/result.
In v2.x/v3.x it was possible to switch between the 2 SRLP URLs to compare the search results from Klevu and native Magento. This can still be done by adding ?klevu_layout_preview=klevu or ?klevu_layout_preview=native to the SRLP URL.
Note all examples below use Module Vendor_Module. Update to match your module. How to create a module.
1. Create the Route
Create a new route so Magento directs the request to your module.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="catalogsearch" frontName="your-search-url">
<module name="Vendor_Module" before="Klevu_FrontendSearch"/>
</route>
</router>
</config>
- The route id MUST be catalogsearch.
- The route frontName is your new search URL, replace your-search-url with your desired URL.
- <module MUST set before="Klevu_FrontendSearch" to replace config in the module Klevu_FrontendSearch. Also ensure your module.xml has a dependency on Klevu_FrontendSearch.
2. Define the Controller Name
Pass the controller name to the LandingUrlProvider. This handles the second part of the request url. Without this, the Klevu plugin will not provide the correct SRLP URL to Klevu when triggering the search.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Klevu\FrontendSearch\Service\Provider\LandingUrlProvider">
<arguments>
<argument name="controllerName" xsi:type="string">controller-name-change-me</argument>
</arguments>
</type>
</config>- controllerName is the directory containing the Action i.e. $this->request->getControllerName().
- By Default controllerName is result. This directs requests to the controller Controller/Result/Index.php i.e. your.site/catalogsearch/result.
- If using Index as the controllerName you can pass / as the controllerName argument. e.g. Controller/Index/Index.php i.e. your.site/search.
3. Inject Conditions To Check if Enabled
If no conditions are provided then the is enabled check will return true.
<type name="Klevu\FrontendSearch\Controller\Result\Index">
<arguments>
<argument name="logger" xsi:type="object">Klevu\Indexing\Logger\Logger</argument>
<argument name="isEnabledConditions" xsi:type="array">
<item name="klevu_integrated"
xsi:type="object">Klevu\Frontend\Service\IsEnabledCondition\IsStoreIntegratedCondition</item>
<item name="klevu_modules_enabled" xsi:type="array">
<item name="klevu_srlp_enabled"
xsi:type="object">Klevu\FrontendSearch\Service\IsEnabledCondition\IsSrlpEnabledCondition</item>
</item>
</argument>
</arguments>
</type>4. Create A Controller To Handle The Request
<?php
declare(strict_types=1);
namespace Vendor\Module\Controller\ControllerNameChangeMe;
use Klevu\Frontend\Exception\InvalidIsEnabledDeterminerException;
use Klevu\Frontend\Exception\OutputDisabledException;
use Klevu\FrontendApi\Service\IsEnabledDeterminerInterface;
use Klevu\FrontendSearch\Service\Provider\QueryTextProviderInterface;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Catalog\Model\Session;
use Magento\CatalogSearch\Controller\Result\Index as CatalogSearchResultIndex;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\State as AppState;
use Magento\Framework\Escaper;
use Magento\Framework\Exception\LocalizedException;
use Magento\Search\Model\QueryFactory;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
class Index extends CatalogSearchResultIndex
implements HttpGetActionInterface, HttpPostActionInterface
{
/**
* @var QueryTextProviderInterface
*/
private QueryTextProviderInterface $queryTextProvider;
public function __construct(
Context $context,
Session $catalogSession,
StoreManagerInterface $storeManager,
QueryFactory $queryFactory,
Resolver $layerResolver,
QueryTextProviderInterface $queryTextProvider,
Escaper $escaper,
LoggerInterface $logger,
AppState $appState,
IsEnabledDeterminerInterface $isEnabledDeterminer,
array $isEnabledConditions = [],
) {
parent::__construct(
context: $context,
catalogSession: $catalogSession,
storeManager: $storeManager,
queryFactory: $queryFactory,
layerResolver: $layerResolver,
);
$this->queryTextProvider = $queryTextProvider;
$this->escaper = $escaper;
$this->logger = $logger;
$this->appState = $appState;
$this->isEnabledDeterminer = $isEnabledDeterminer;
$this->isEnabledConditions = $isEnabledConditions;
}
/**
* @return void
*/
public function execute()
{
if (!$this->isKlevuEnabled()) {
parent::execute();
return;
}
$this->setPageTitle();
$this->_view->renderLayout();
}
/**
* @return void
*/
private function setPageTitle(): void
{
$query = $this->queryTextProvider->get();
$this->_view->loadLayout();
$page = $this->_view->getPage();
$config = $page->getConfig();
$title = $config->getTitle();
$title->set(
__("Search results for: '%1'", $query)->render(),
);
}
/**
* @return bool
* @throws InvalidIsEnabledDeterminerException
*/
private function isKlevuEnabled(): bool
{
$return = false;
try {
$this->isEnabledDeterminer->executeAnd($this->isEnabledConditions);
$return = true;
} catch (InvalidIsEnabledDeterminerException $exception) {
if ($this->appState->getMode() !== AppState::MODE_PRODUCTION) {
throw $exception;
}
$this->logger->error(
message: 'Method: {method}, Error: {error}',
context: [
'method' => __METHOD__,
'error' => $exception->getMessage(),
],
);
} catch (OutputDisabledException) {
// Output is disabled.
}
return $return;
}
}
- Change ControllerNameChangeMe in the Controller path and namespace to match that set in LandingUrlProvider controllerName argument in etc/di.xml (see previous step).