1656708600
RSSはReallySimpleSyndicationの略で、RSSフィードはWebサイトの更新されたURLのリストを含むXMLファイルです。RSSフィードを使用して、電子メール通知の更新を自動送信できます。
この例では、タイトル、スラッグ、本文を含む投稿テーブルを作成します。次に、ダミーポストのファクトリを作成します。次に、XMLファイルを生成し、投稿のすべてのURLを一覧表示します。これは非常に基本的な例です。それでは、フォローしてみましょう。Webサイトのサイトマップファイルを取得して、Webマスターのツールに送信します。
以下の手順に従いましょう。
ステップ1:Laravelをインストールする
この手順は必須ではありません。ただし、laravelアプリを作成していない場合は、先に進んで以下のコマンドを実行できます。
composer create-project laravel/laravel example-app
ステップ2:移行後とモデルを作成する
このステップでは、移行とモデルを作成します。それでは、以下のコマンドを実行して投稿テーブルを作成しましょう。
php artisan make:migration create_posts_table
次に、以下のコードを移行ファイルに簡単に更新します。
database / migrations / create_posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
次に、以下のコマンドで作成された新しい移行を実行します。
php artisan migrate
次に、以下のコマンドを実行してPostモデルを作成します。
php artisan make:model Post
次に、次のコードをPostモデルに更新します。
app / Models / Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title', 'slug', 'body'
];
}
ステップ3:ポストファクトリーを作成する
このステップでは、Postファクトリクラスを作成し、tinkerコマンドを使用してダミーレコードを生成します。それでは、以下のコマンドを実行してポストファクトリを作成しましょう。
php artisan make:factory PostFactory
次に、以下のコードをコピーして、PostFactory.phpファイルを更新します。
database / factorys / PostFactory.php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Post;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
*/
class PostFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $this->faker->text(),
'slug' => Str::slug($this->faker->text()),
'body' => $this->faker->paragraph()
];
}
}
次に、いじくり回すコマンドを実行して、ダミーの投稿を作成します。
php artisan tinker
App\Models\Post::factory()->count(30)->create();
ステップ4:ルートを作成する
このステップでは、1つのルートsitemap.xmlを作成します。追加しましょう。
ルート/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RSSFeedController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('feed', [RSSFeedController::class, 'index']);
ステップ5:コントローラーを作成する
このステップでは、index()を使用してRSSFeedControllerとして新しいコントローラーを作成する必要があります。すべての投稿を取得し、ブレードファイルに渡します。応答をxmlファイルとして返します。それでは、次のコードを更新しましょう:
app / Http / Controllers / RSSFeedController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class RSSFeedController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$posts = Post::latest()->get();
return response()->view('rss', [
'posts' => $posts
])->header('Content-Type', 'text/xml');
}
}
ステップ6:ビューファイルを作成する
最後のステップでは、すべての投稿を表示するためのrss.blade.phpを作成し、次のコードを配置しましょう。
resources / views / rss.blade.php
<?=
'<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL
?>
<rss version="2.0">
<channel>
<title><![CDATA[ ]]></title>
<link><![CDATA[ https://your-website.com/feed ]]></link>
<description><![CDATA[ Your website description ]]></description>
<language>en</language>
<pubDate>{{ now() }}</pubDate>
@foreach($posts as $post)
<item>
<title><![CDATA[{{ $post->title }}]]></title>
<link>{{ $post->slug }}</link>
<description><![CDATA[{!! $post->body !!}]]></description>
<category>{{ $post->category }}</category>
<author><![CDATA[Hardk Savani]]></author>
<guid>{{ $post->id }}</guid>
<pubDate>{{ $post->created_at->toRssString() }}</pubDate>
</item>
@endforeach
</channel>
</rss>
Laravelアプリを実行する:
必要な手順はすべて完了しました。次に、以下のコマンドを入力し、Enterキーを押してLaravelアプリを実行する必要があります。
php artisan serve
次に、Webブラウザーに移動し、指定されたURLを入力して、アプリの出力を表示します。
http://localhost:8000/feed
お役に立てば幸いです...
ソース:https ://www.itsolutionstuff.com/post/laravel-9-create-rss-feed-example-tutorialexample.html
1656708600
RSSはReallySimpleSyndicationの略で、RSSフィードはWebサイトの更新されたURLのリストを含むXMLファイルです。RSSフィードを使用して、電子メール通知の更新を自動送信できます。
この例では、タイトル、スラッグ、本文を含む投稿テーブルを作成します。次に、ダミーポストのファクトリを作成します。次に、XMLファイルを生成し、投稿のすべてのURLを一覧表示します。これは非常に基本的な例です。それでは、フォローしてみましょう。Webサイトのサイトマップファイルを取得して、Webマスターのツールに送信します。
以下の手順に従いましょう。
ステップ1:Laravelをインストールする
この手順は必須ではありません。ただし、laravelアプリを作成していない場合は、先に進んで以下のコマンドを実行できます。
composer create-project laravel/laravel example-app
ステップ2:移行後とモデルを作成する
このステップでは、移行とモデルを作成します。それでは、以下のコマンドを実行して投稿テーブルを作成しましょう。
php artisan make:migration create_posts_table
次に、以下のコードを移行ファイルに簡単に更新します。
database / migrations / create_posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
次に、以下のコマンドで作成された新しい移行を実行します。
php artisan migrate
次に、以下のコマンドを実行してPostモデルを作成します。
php artisan make:model Post
次に、次のコードをPostモデルに更新します。
app / Models / Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title', 'slug', 'body'
];
}
ステップ3:ポストファクトリーを作成する
このステップでは、Postファクトリクラスを作成し、tinkerコマンドを使用してダミーレコードを生成します。それでは、以下のコマンドを実行してポストファクトリを作成しましょう。
php artisan make:factory PostFactory
次に、以下のコードをコピーして、PostFactory.phpファイルを更新します。
database / factorys / PostFactory.php
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Post;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
*/
class PostFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'title' => $this->faker->text(),
'slug' => Str::slug($this->faker->text()),
'body' => $this->faker->paragraph()
];
}
}
次に、いじくり回すコマンドを実行して、ダミーの投稿を作成します。
php artisan tinker
App\Models\Post::factory()->count(30)->create();
ステップ4:ルートを作成する
このステップでは、1つのルートsitemap.xmlを作成します。追加しましょう。
ルート/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RSSFeedController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('feed', [RSSFeedController::class, 'index']);
ステップ5:コントローラーを作成する
このステップでは、index()を使用してRSSFeedControllerとして新しいコントローラーを作成する必要があります。すべての投稿を取得し、ブレードファイルに渡します。応答をxmlファイルとして返します。それでは、次のコードを更新しましょう:
app / Http / Controllers / RSSFeedController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class RSSFeedController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$posts = Post::latest()->get();
return response()->view('rss', [
'posts' => $posts
])->header('Content-Type', 'text/xml');
}
}
ステップ6:ビューファイルを作成する
最後のステップでは、すべての投稿を表示するためのrss.blade.phpを作成し、次のコードを配置しましょう。
resources / views / rss.blade.php
<?=
'<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL
?>
<rss version="2.0">
<channel>
<title><![CDATA[ ]]></title>
<link><![CDATA[ https://your-website.com/feed ]]></link>
<description><![CDATA[ Your website description ]]></description>
<language>en</language>
<pubDate>{{ now() }}</pubDate>
@foreach($posts as $post)
<item>
<title><![CDATA[{{ $post->title }}]]></title>
<link>{{ $post->slug }}</link>
<description><![CDATA[{!! $post->body !!}]]></description>
<category>{{ $post->category }}</category>
<author><![CDATA[Hardk Savani]]></author>
<guid>{{ $post->id }}</guid>
<pubDate>{{ $post->created_at->toRssString() }}</pubDate>
</item>
@endforeach
</channel>
</rss>
Laravelアプリを実行する:
必要な手順はすべて完了しました。次に、以下のコマンドを入力し、Enterキーを押してLaravelアプリを実行する必要があります。
php artisan serve
次に、Webブラウザーに移動し、指定されたURLを入力して、アプリの出力を表示します。
http://localhost:8000/feed
お役に立てば幸いです...
ソース:https ://www.itsolutionstuff.com/post/laravel-9-create-rss-feed-example-tutorialexample.html