首页 > 解决方案 > RSS API 能够使用 Laravel 添加、删除和列出所有 RSS 资源

问题描述

下午好,我必须做一项原则上看起来很容易的任务。

任务是创建一个 API,允许您列出、添加和删除 RSS 源,并获取提要的最后内容。如果 RSS 源离线,我还必须找到一种方法来返回旧内容。

我已经完成了资源模型和控制器、迁移和 WEB 路由(我知道我必须将路由放在 api.php 中,但我想通过前端确保它返回我想要的所有数据,然后我将使用 Postman 对其进行测试),但我被阻止了,因为我现在不知道如何进行。

我将下面的代码留给您以了解我在做什么:

web.php(未来的 api.php):

Route::get('/', function () {
    return view('welcome');
});

//Resource routes:
Route::get('/resources/{resource}', 'ResourcesController@show')
    ->name('resources.show');

Route::feeds();

资源控制器.php:

namespace App\Http\Controllers;

use App\Resource;
use Illuminate\Http\Request;

class ResourcesController extends Controller
{
    public function show(Request $request, Resource $resource)
    {
        return $resource;
    }
}

资源.php:

namespace App;

use Spatie\Feed\Feedable;
use Spatie\Feed\FeedItem;
use Illuminate\Database\Eloquent\Model;

class Resource extends Model implements Feedable
{
    public function toFeedResource()
    {
        return FeedItem::create()
        ->id($this->id)
        ->title($this->title)
        ->content($this->description)
        ->updated($this->updated_at)
        ->link($this->link)
        ->source($this->source);
    }

    public static function getResourceItems()
    {
        return static::all();
    }

    public function getLinkAttribute()
    {
        return route('resources.show', $this);
    }
}

create_resources_table.php:

    public function up()
    {
        Schema::create('resources', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('description');
            $table->string('source');
            $table->timestamps();
        });
    }

欢迎任何帮助,并提前致谢。

标签: phplaravelapirssrss-reader

解决方案


推荐阅读