ProcessWire API для керування контентом
ProcessWire — PHP CMS з власним API для роботи з контентом прямо у PHP-шаблонах. API базується на чеклистах методів ($pages->find()) та дозволяє складні запити без SQL. Для headless-режиму використовуйте модуль ProcessWire GraphQL або кастомний REST endpoint.
Вбудований PHP API
ProcessWire використовує $pages, $page, $user, $config як глобальні змінні в шаблонах:
// templates/blog.php
// Вибір з фільтрами та пагінацією
$limit = 12;
$start = ($input->pageNum - 1) * $limit;
$posts = $pages->find("template=blog-post, status=published, sort=-date, limit=$limit, start=$start");
$totalPosts = $pages->count("template=blog-post, status=published");
// Вибір з умовами по полях
$featuredPosts = $pages->find("
template=blog-post,
featured=1,
date>=today,
category.name%=Web Development,
sort=-date,
limit=3
");
// Один елемент
$post = $pages->get("template=blog-post, slug=my-post-slug");
if (!$post->id) wire404();
Selector String — мова запитів
// Текстовий пошук
$pages->find("template=product, title*=laptop, sort=title");
// Числові умови
$pages->find("template=product, price>=1000, price<=5000");
// Дата
$pages->find("template=event, event_date>=today, sort=event_date");
// OR-умови
$pages->find("template=post, (category=tech|category=science)");
// Пов'язані сторінки
$pages->find("template=product, categories.id={$category->id}");
// Повнотекстовий пошук
$pages->find("title|body~=search query, template=post");
// Сортування за кастомним полем
$pages->find("template=product, sort=-rating, sort=title");
REST API через template-router
ProcessWire не має вбудованого REST API. Створіть через template-router:
// site/templates/api.php
// URL: /api/blog/?page=1&limit=10
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: ' . $config->httpHost);
// Проста авторизація за ключем
$apiKey = $input->get->text('key');
if ($apiKey !== $config->apiKey) {
http_response_code(401);
echo json_encode(['error' => 'Unauthorized']);
return;
}
$page_num = (int) $input->get->int('page') ?: 1;
$limit = min((int) $input->get->int('limit') ?: 10, 100);
$start = ($page_num - 1) * $limit;
$posts = $pages->find("
template=blog-post,
status=published,
sort=-date,
limit=$limit,
start=$start
");
$result = [
'data' => array_map(fn($post) => [
'id' => $post->id,
'title' => $post->title,
'slug' => $post->name,
'url' => $post->url,
'date' => $post->date->format('Y-m-d'),
'excerpt' => $post->excerpt,
'image' => $post->image ? [
'url' => $post->image->width(800)->url,
'width' => 800,
'height' => (int) round(800 / $post->image->ratio),
] : null,
], $posts->getArray()),
'total' => $posts->getTotal(),
'page' => $page_num,
'limit' => $limit,
];
echo json_encode($result);
Модуль ProcessWire GraphQL
# Установка через модулі ProcessWire
# Скачати з processwire.com/modules/processwire-graphql/
# В config.php — налаштування доступу
$config->graphql = [
'templateFilters' => ['blog-post', 'product', 'category'],
'fieldFilters' => ['title', 'body', 'date', 'image', 'category'],
'maxLimit' => 100,
];
query {
blogPost(s: "status=published, sort=-date, limit=10") {
list {
id
title
date
body
image { url(width: 800) }
category { title url }
}
total
}
}
Кешування відповідей
// Кеш через WireCache
$cacheKey = "api_posts_page{$page_num}";
$cached = $cache->get($cacheKey);
if ($cached) {
echo $cached;
return;
}
// ... формування $result ...
$json = json_encode($result);
$cache->save($cacheKey, $json, 3600); // 1 час
echo $json;
Створення базового REST API на ProcessWire (5–7 endpoints) — 2–4 дні.







