Merge branch 'feature/seo-listing-pages' into dev
This commit is contained in:
commit
9d3b21359e
4 files changed
+130
-10
No files matched your search
@@ -80,17 +80,17 @@ class NewsController extends Controller
|
||||
|
||||
public function taglist(Request $request, $tag)
|
||||
{
|
||||
return $this->listNews($request, 'tag/' . $tag, ucfirst($tag));
|
||||
return $this->listNews($request, 'tag/' . $tag, ucfirst($tag), 'items', null, $tag);
|
||||
}
|
||||
|
||||
public function regionlist(Request $request, $region)
|
||||
{
|
||||
return $this->listNews($request, 'tag/' . $region, ucfirst($region));
|
||||
return $this->listNews($request, 'tag/' . $region, ucfirst($region), 'items', null, $region);
|
||||
}
|
||||
|
||||
public function themelist(Request $request, $theme)
|
||||
{
|
||||
return $this->listNews($request, 'tag/' . $theme, ucfirst($theme));
|
||||
return $this->listNews($request, 'tag/' . $theme, ucfirst($theme), 'items', null, $theme);
|
||||
}
|
||||
|
||||
public function search(Request $request, $query)
|
||||
@@ -133,7 +133,11 @@ class NewsController extends Controller
|
||||
return abort();
|
||||
}
|
||||
|
||||
private function listNews(Request $request, $url, $title = null, $id = 'items', $total = null)
|
||||
/**
|
||||
* @param string|null $slug tag slug of a tag/region/theme page; used to show
|
||||
* the tag's real name ('Gooise Meren' instead of 'Gooise-meren')
|
||||
*/
|
||||
private function listNews(Request $request, $url, $title = null, $id = 'items', $total = null, $slug = null)
|
||||
{
|
||||
if ($request->ajax()) {
|
||||
$total = 5;
|
||||
@@ -147,6 +151,9 @@ class NewsController extends Controller
|
||||
foreach ($apiResult->news ?? [] as $newsItem) {
|
||||
$news[] = new \Model\NewsItem($newsItem);
|
||||
}
|
||||
if ($slug) {
|
||||
$title = self::tagTitle($news, $slug) ?: $title;
|
||||
}
|
||||
|
||||
$populair = [];
|
||||
if ($title == null) {
|
||||
@@ -176,7 +183,60 @@ class NewsController extends Controller
|
||||
$newsItems[] = new \Model\NewsItem($_newsItem);
|
||||
}
|
||||
|
||||
return view($request->ajax() ? ($title == null ? 'partial/home_newslist_small' : 'partial/newslist_small') : ($title == null ? 'news' : 'newslist'), ['populair' => $populair, 'newsItems' => $newsItems, 'podcasts' => $podcasts, 'podcast' => $podcast, 'id' => $id, 'title' => $title, 'news' => $news, 'searchURL' => 'nieuws/zoeken']);
|
||||
$data = ['populair' => $populair, 'newsItems' => $newsItems, 'podcasts' => $podcasts, 'podcast' => $podcast, 'id' => $id, 'title' => $title, 'news' => $news, 'searchURL' => 'nieuws/zoeken'];
|
||||
if (!$request->ajax() && $title !== null) {
|
||||
$data += $this->listingSeo($url, $title, $news, max(1, $page));
|
||||
}
|
||||
|
||||
return view($request->ajax() ? ($title == null ? 'partial/home_newslist_small' : 'partial/newslist_small') : ($title == null ? 'news' : 'newslist'), $data);
|
||||
}
|
||||
|
||||
/** The tag's name as used in the articles, e.g. 'Gooise Meren' for slug 'gooise-meren'. */
|
||||
private static function tagTitle(array $news, $slug)
|
||||
{
|
||||
foreach ($news as $item) {
|
||||
foreach ((is_array($item->tags) ? $item->tags : []) as $tag) {
|
||||
if (($tag->slug ?? null) === $slug && !empty($tag->titel)) {
|
||||
return $tag->titel;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* SEO data for news overview pages (tag/region/theme pages and search results):
|
||||
* canonical URL including the page number, description, Open Graph tags and
|
||||
* whether to add structured data. Search results are kept out of the index.
|
||||
*/
|
||||
private function listingSeo($url, $title, array $news, $page)
|
||||
{
|
||||
$isSearch = strpos($url, 'zoeken/') === 0;
|
||||
$canonical = url()->current() . ($page > 1 ? '?pagina=' . $page : '');
|
||||
$description = 'Het laatste nieuws over ' . $title . ' van GOOI&meer, de streekomroep voor Gooi & Vechtstreek.'
|
||||
. ($page > 1 ? ' Pagina ' . $page . '.' : '');
|
||||
|
||||
$image = null;
|
||||
foreach ($news as $item) {
|
||||
if ($images = $item->imageList()) {
|
||||
$image = $images[0]['url'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
$metadata = (new \Model\MetaData())
|
||||
->set('type', 'website')
|
||||
->set('title', $title . ' - Nieuws' . ($page > 1 ? ' (pagina ' . $page . ')' : ''))
|
||||
->set('description', $isSearch ? null : $description)
|
||||
->set('image', $image);
|
||||
|
||||
return [
|
||||
'canonical' => $canonical,
|
||||
'pageNumber' => $page,
|
||||
'metadata' => $metadata,
|
||||
'noindex' => $isSearch,
|
||||
'listingJsonld' => !$isSearch,
|
||||
];
|
||||
}
|
||||
|
||||
public function popular()
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
<head>
|
||||
@include('layouts._analytics')
|
||||
<title>{!! trim($__env->yieldContent('title')) !!} | GOOI&meer</title>
|
||||
<title>{!! trim($__env->yieldContent('title')) !!}@if(($pageNumber ?? 1) > 1) (pagina {{ $pageNumber }})@endif | GOOI&meer</title>
|
||||
<!--meta-->
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.2" />
|
||||
<meta name="format-detection" content="telephone=no" />
|
||||
<meta name="description" content="{{ (isset($metadata) && $metadata->get('description')) ? $metadata->get('description') : 'GOOI&meer is de streekomroep voor Hilversum, Huizen, Blaricum, Eemnes en Laren.' }}" />
|
||||
{{-- Allow large image previews (Google Discover, Top Stories) --}}
|
||||
<meta name="robots" content="max-image-preview:large" />
|
||||
<link rel="canonical" href="{{ url()->current() }}" />
|
||||
{{-- Search results are kept out of the index; large image previews for the rest --}}
|
||||
<meta name="robots" content="{{ !empty($noindex) ? 'noindex, follow' : 'max-image-preview:large' }}" />
|
||||
{{-- Pages can set $canonical (e.g. overview pages: including ?pagina=N) --}}
|
||||
<link rel="canonical" href="{{ $canonical ?? url()->current() }}" />
|
||||
<!--style-->
|
||||
<link href='//fonts.googleapis.com/css?family=Roboto:300,400,700' rel='stylesheet' type='text/css'>
|
||||
<link href='//fonts.googleapis.com/css?family=Roboto+Condensed:300,400,700' rel='stylesheet' type='text/css'>
|
||||
@@ -32,7 +34,7 @@
|
||||
<meta property="fb:app_id" content="133349980094758" />
|
||||
<meta property="og:site_name" content="GOOI&meer" />
|
||||
<?php if(isset($metadata)) {
|
||||
$metadata->set('url', url()->current())
|
||||
$metadata->set('url', $canonical ?? url()->current())
|
||||
->prepend('image', $imgBase)
|
||||
->prepend('audio', route('gemist.fragment'))
|
||||
->append('title', ' | GOOI&meer')
|
||||
|
||||
@@ -9,6 +9,12 @@
|
||||
@section('page_class')
|
||||
news_post post_container
|
||||
@endsection
|
||||
|
||||
@if(!empty($listingJsonld))
|
||||
@push('head')
|
||||
@include('partial/listing_jsonld', ['title' => $title, 'news' => $news, 'canonical' => $canonical, 'pageNumber' => $pageNumber])
|
||||
@endpush
|
||||
@endif
|
||||
@section('page_container_class')
|
||||
{{-- 'news': same list rows as on /nieuws --}}
|
||||
grey_background news
|
||||
@@ -38,7 +44,8 @@
|
||||
|
||||
@if(count($news) >= 15)
|
||||
<div>
|
||||
<a class="btn auto_width" id="meer-nieuws" href="#"
|
||||
{{-- The JavaScript loads more items in place; the link to the next page is for search engines --}}
|
||||
<a class="btn auto_width" id="meer-nieuws" href="?pagina={{ ($pageNumber ?? 1) + 1 }}"
|
||||
data-loadmorenews='{"container":["#items-more-news"]}'>
|
||||
<span class="fas fa-spinner fa-spin" id="loading"></span>
|
||||
LAAD MEER NIEUWSBERICHTEN
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
{{--
|
||||
Structured data (schema.org JSON-LD) for a news overview page (tag, region,
|
||||
theme): a CollectionPage with the articles on this page as an ItemList, and
|
||||
the breadcrumb. Parameters: $title, $news, $canonical, $pageNumber.
|
||||
--}}
|
||||
@php
|
||||
$home = rtrim(url('/'), '/') . '/';
|
||||
$firstPage = strtok($canonical, '?');
|
||||
// Position across pages: the articles on page N follow those of the earlier pages
|
||||
$offset = ($pageNumber - 1) * count($news);
|
||||
|
||||
$items = [];
|
||||
foreach (array_values($news) as $i => $item) {
|
||||
$items[] = [
|
||||
'@type' => 'ListItem',
|
||||
'position' => $offset + $i + 1,
|
||||
'url' => url($item->url),
|
||||
'name' => $item->plainTitle(),
|
||||
];
|
||||
}
|
||||
|
||||
$collection = [
|
||||
'@type' => 'CollectionPage',
|
||||
'@id' => $canonical . '#collection',
|
||||
'url' => $canonical,
|
||||
'name' => $title . ' - Nieuws' . ($pageNumber > 1 ? ' (pagina ' . $pageNumber . ')' : ''),
|
||||
'description' => 'Het laatste nieuws over ' . $title . ' van GOOI&meer, de streekomroep voor Gooi & Vechtstreek.',
|
||||
'about' => ['@type' => 'Thing', 'name' => $title],
|
||||
'inLanguage' => 'nl-NL',
|
||||
'isPartOf' => ['@id' => $home . '#website'],
|
||||
'publisher' => ['@id' => $home . '#organization'],
|
||||
'mainEntity' => [
|
||||
'@type' => 'ItemList',
|
||||
'itemListOrder' => 'https://schema.org/ItemListOrderDescending',
|
||||
'numberOfItems' => count($items),
|
||||
'itemListElement' => $items,
|
||||
],
|
||||
];
|
||||
|
||||
$breadcrumb = [
|
||||
'@type' => 'BreadcrumbList',
|
||||
'itemListElement' => [
|
||||
['@type' => 'ListItem', 'position' => 1, 'name' => 'Home', 'item' => $home],
|
||||
['@type' => 'ListItem', 'position' => 2, 'name' => 'Nieuws', 'item' => route('nieuws')],
|
||||
['@type' => 'ListItem', 'position' => 3, 'name' => $title, 'item' => $firstPage],
|
||||
],
|
||||
];
|
||||
|
||||
$jsonld = ['@context' => 'https://schema.org', '@graph' => [$collection, $breadcrumb]];
|
||||
@endphp
|
||||
<script type="application/ld+json">{!! json_encode($jsonld, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_PRETTY_PRINT) !!}</script>
|
||||
Reference in new issue
Block a user