Ask Your Question

Revision history [back]

Yes, it is possible to dynamically include the search phrase in the headers of your search result page in Yii. You can use the Yii::$app->view->registerMetaTag() method to add a meta tag with the search phrase as its content.

For example, in your controller action that displays the search result page, you can add the following code:

public function actionSearch($q)
{
    // Perform search and get search results

    // Set the page title with the search query
    $this->view->title = 'Search results for '.$q;

    // Add a meta tag with the search query as content
    Yii::$app->view->registerMetaTag([
        'name' => 'description',
        'content' => 'Search results for '.$q
    ]);

    // Render the search result page with the search results
    return $this->render('search', [
        'results' => $results,
        'query' => $q
    ]);
}

In the example code above, the $q parameter is the search query. The page title is set to 'Search results for '.$q, and a meta tag is added with the name 'description' and the content 'Search results for '.$q. This will dynamically include the search phrase in the page headers.