Dalam posting ini, saya akan menjelaskan cara membuat RSS feed.
Kode Terpisah dan Penjelasan
Pertama buat rute yang akan merespon dengan umpan, dalam contoh ini saya menggunakan pengontrol bernama BlogController dan metode bernama umpan.
Route::get('feed', 'BlogController@feed');
Dalam kueri metode feed Anda, sebuah model , dalam hal ini mengurutkan berdasarkan kolom created_at dan mengambil 20 catatan pertama, meneruskan data ke view :
public function feed()
{
$posts = Post::orderBy('created_at', 'desc')->take(20)->get();
return view('feed')->with(compact('posts'));
}
Di bagian atas tampilan, gunakan header Permintaan untuk menyetel tipe konten ke application/xml
Mulai rss dan tag saluran, atur judul, deskripsi, dan url tautan
{{ Request::header('Content-Type : application/xml') }}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{{ config('app.name') }}</title>
<description>RSS Feed</description>
<link>{{ url('/') }}</link>
Loop berikutnya melalui data
@foreach ($posts as $post)
Sekarang buat blok blade php, di dalam @php menggunakan str_replace swap & dan & dan chate quotes.
Hapus semua garis miring dari title , description , and slug.
Opsional gambar dapat disematkan, dalam hal ini, pemeriksaan dilakukan untuk melihat apakah kolom gambar tidak kosong, jika demikian buatlah tag gambar.
@php
$post->title = str_replace("&", "&", $post->title);
$post->description = str_replace("”", "”", $post->description);
$post->description = str_replace("“", "“", $post->description);
$post->title = stripslashes($post->title);
$post->description = stripslashes($post->description);
$post->slug = stripslashes($post->slug);
if ($post->image !='') {
$img = "<img src='".url($post->image)."' alt='".$post->title."' width='600'>";
} else {
$img = null;
}
@endphp
Selanjutnya, buat tag item dan tampilkan tag berikut:
judul - judul posting
description - menggunakan CDATA menampilkan variabel img dan deskripsi
pubDate - tanggal posting dibuat menggunakan date & strtotime untuk memformat tanggal terakhir mengatur zona waktu.
link and guid - url ke pos
atomLlink - tautan ke pos
CATATAN : Saya menggunakan kolom yang disebut $slug ini adalah bidang khusus yang menyimpan versi judul yang ramah mesin pencari.
<item>
<title>{{ $post->title }}</title>
<description><![CDATA[{!! $img !!} {!! $post->description !!}]]></description>
<pubDate>{{ date('D, d M Y H:i:s', strtotime($post->created_at)) }} GMT</pubDate>
<link>{{ url($post->slug) }}</link>
<guid>{{ url($post->slug) }}</guid>
<atom:link href="{{ url($post->slug) }}" rel="self" type="application/rss+xml"/>
</item>
Terakhir, tutup foreach dan tag.
@endforeach
</channel>
</rss>
Kode Lengkap
{{ Request::header('Content-Type : application/xml') }}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{{ config('app.name') }}</title>
<description>RSS Feed</description>
<link>{{ url('/') }}</link>
@foreach ($posts as $post)
@php
$post->title = str_replace("&", "&", $post->title);
$post->description = str_replace("”", "”", $post->description);
$post->description = str_replace("“", "“", $post->description);
$post->title = stripslashes($post->title);
$post->description = stripslashes($post->description);
$post->slug = stripslashes($post->slug);
if ($post->image !='') {
$img = "<img src='".url($post->image)."' alt='".$post->title."' width='600'>";
} else {
$img = null;
}
@endphp
<item>
<title>{{ $post->title }}</title>
<description><![CDATA[{!! $img !!} {!! $post->description !!}]]></description>
<pubDate>{{ date('D, d M Y H:i:s', strtotime($post->created_at)) }} GMT</pubDate>
<link>{{ url($post->slug) }}</link>
<guid>{{ url($post->slug) }}</guid>
<atom:link href="{{ url($post->slug) }}" rel="self" type="application/rss+xml"/>
</item>
@endforeach
</channel>
</rss>
Langkah opsional adalah menambahkan tag tautan ke bagian head html Anda.
<link href='{{ url('feed') }}' rel='alternate' title='RSS' type='application/rss+xml'/>