imarc/craft-googlecustomsearch
Composer 安装命令:
composer require imarc/craft-googlecustomsearch
包简介
A Craft plugin for integrating with Google's Custom Search (and Google's Site Search.)
README 文档
README
A Craft CMS plugin that incorporates a Google Custom Search into your website.
Google offers free and paid tiers for accessing their search results.
If you are looking for the Craft 2 version of this plugin, see the craft2 branch.
Requirements
This plugin requires Craft CMS 3.0.0, 4.x, or 5.x and PHP 8.0+ (PHP 8.2+ recommended for Craft 5).
Installation
To install the plugin, follow these instructions.
-
Open your terminal and go to your Craft project:
cd /path/to/project -
Then tell Composer to load the plugin:
composer require imarc/craft-googlecustomsearch -
In the Control Panel, go to Settings → Plugins and click the “Install” button for googlecustomsearch.
Configuring
You will need a Search Engine ID and API Key from Google.
Search Engine ID - On the Custom Search Engine Control Panel, create a new search engine for the site you would like to integrate. Once created, you can retrieve your Search Engine ID from the Setup tab.
API Key - If you're using the free tier, visit the Google Developers Console and create a project for your search engine. Within your project, you’ll need to enable the Custom Search API from the APIs tab. Finally, on the Credentials tab, you will need to create a Public API access key by selecting the Create new Key option and choosing Server key. The API Key will now be available. If you're using a paid tier, you can find your API key in the Control Panel in the Business > XML & JSON tab.
The credentials can either be added from Craft's plugin settings or within config/googlecustomsearch.php.
On multisite installs, plugin settings can be configured per site from Settings → Plugins → Google Custom Search. Use the site menu to switch between sites.
Config file
Single-site (or shared credentials):
<?php return [ 'apiKey' => getenv('GOOGLE_SEARCH_API_KEY'), 'searchEngineId' => getenv('GOOGLE_SEARCH_ENGINE_ID'), ];
Multisite — per-site values in the config file (keys are site handles):
<?php return [ 'siteSettings' => [ 'default' => [ 'apiKey' => getenv('GOOGLE_SEARCH_API_KEY'), 'searchEngineId' => getenv('GOOGLE_SEARCH_ENGINE_ID'), ], 'fr' => [ 'apiKey' => getenv('GOOGLE_SEARCH_API_KEY_FR'), 'searchEngineId' => getenv('GOOGLE_SEARCH_ENGINE_ID_FR'), ], ], ];
You can also index top-level keys by site handle; Craft will resolve them for the current site:
<?php return [ 'apiKey' => [ 'default' => getenv('GOOGLE_SEARCH_API_KEY'), 'fr' => getenv('GOOGLE_SEARCH_API_KEY_FR'), ], 'searchEngineId' => [ 'default' => getenv('GOOGLE_SEARCH_ENGINE_ID'), 'fr' => getenv('GOOGLE_SEARCH_ENGINE_ID_FR'), ], ];
Usage
In your twig template, retrieve search results from Google by passing it your search query, which you can then iterate over to display:
{% set response = craft.googlecustomsearch.performSearch('google') %}
Here is a complete example with pagination:
{% extends "_layout" %}
{% set query = craft.request.getParam('q') %}
{% set page = craft.request.getParam('page') ?: '1' %}
{% set title = "Search" %}
{% if query %}
{% set response = craft.googlecustomsearch.performSearch(query, page) %}
{% set title = query ~ " - Search" %}
{% set totalPages = ceil(response.totalResults / response.perPage) %}
{% endif %}
{% block content %}
<div class="main">
<h1>Search</h1>
<form class="search">
<div class="text">
<input type="search" name="q" placeholder="Search" value="{{ query }}">
</div>
<div class="submit">
<input type="submit" value="Search">
</div>
</form>
{% if query %}
{% if response.results | length %}
<div class="intro">
<p>
Showing {{ response.start }}–{{ response.end }} of {{ response.totalResults }} results for <strong>{{ query }}</strong>
</p>
</div>
<ul class="listing">
{% for result in response.results %}
<li>
<h3>
<a href="{{ result.link }}">
{{ result.title | raw }}
</a>
</h3>
{% if result.thumbnail | length %}
<img src="{{ result.thumbnail }}" width="80" style="float: left; margin: 0 1em 1em 0" />
{% endif %}
<a class="url" href="{{ result.link }}">{{ result.link }}</a>
<p class="summary">
{{ result.htmlSnippet | raw }}
</p>
</li>
{% endfor %}
</ul>
{% if totalPages > 1 %}
<div class="meta paginator">
{% if page > 1 %}
<a href="{{ url('search', {q:query, page:(page-1)}) }}" class="prev"></i>Previous</a>
{% endif %}
{% if page < totalPages %}
<a href="{{ url('search', {q:query, page:(page+1)}) }}" class="next">Next</a>
{% endif %}
</div>
{% endif %}
{% else %}
<div class="info">
<p>
Your search for “{{ query }}” didn’t return any results.
</p>
</div>
{% endif %}
{% endif %}
</div>
{% endblock %}
Credits
Brought to you by Imarc
imarc/craft-googlecustomsearch 适用场景与选型建议
imarc/craft-googlecustomsearch 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.9k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2016 年 04 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Craft」 「google custom search」 「craftcms」 「craft-plugin」 「craft3」 「google site search」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 imarc/craft-googlecustomsearch 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 imarc/craft-googlecustomsearch 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 imarc/craft-googlecustomsearch 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Expand, collapse, change the status of, or delete multiple blocks in a Matrix field simultaneously.
GraphQL authentication for your headless Craft CMS applications.
Supercharged text field validation.
Multiauth and custom grants for laravel passport
Integrate with Snipcart.
Custom SilverStripe tweaks to keep things tidy.
统计信息
- 总下载量: 12.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 3
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2016-04-20