Laravel 5 Feeds
, (*1)
, (*2)
A simple Laravel 5 service provider for including the SimplePie library., (*3)
Installation
The Laravel 5 Feeds Service Provider can be installed via Composer by requiring the
willvincent/feeds
package in your project's composer.json
., (*4)
{
"require": {
"willvincent/feeds": "1.1.*"
}
}
Configuration
To use the Feeds Service Provider, you must register the provider when bootstrapping your Laravel application., (*5)
Find the providers
key in your config/app.php
and register the Service Provider., (*6)
'providers' => [
// ...
willvincent\Feeds\FeedsServiceProvider::class,
],
Find the aliases
key in your config/app.php
and register the Facade., (*7)
'aliases' => [
// ...
'Feeds' => willvincent\Feeds\Facades\FeedsFacade::class,
],
Usage
Run php artisan vendor:publish --provider="willvincent\Feeds\FeedsServiceProvider"
to publish the default config file, edit caching setting withing the resulting config/feeds.php
file as desired., (*8)
See SimplePie Documentation for full API usage documentation., (*9)
The make() accepts 3 paramaters, the first parameter is an array of feed URLs, the second parameter is the max number of items to be returned per feed, and while the third parameter is a boolean which you can set to force to read unless content type not a valid RSS., (*10)
$feed = Feeds::make('http://feed/url/goes/here');
Note: In Laravel 5, Facades must either be prefixed with a backslash, or brought into scope with a use [facadeName]
declaration.
Controller:, (*11)
public function demo() {
$feed = Feeds::make('http://blog.case.edu/news/feed.atom');
$data = array(
'title' => $feed->get_title(),
'permalink' => $feed->get_permalink(),
'items' => $feed->get_items(),
);
return View::make('feed', $data);
}
or Force to read unless content type not a valid RSS, (*12)
public function demo() {
$feed = Feeds::make('http://blog.case.edu/news/feed.atom', true); // if RSS Feed has invalid mime types, force to read
$data = array(
'title' => $feed->get_title(),
'permalink' => $feed->get_permalink(),
'items' => $feed->get_items(),
);
return View::make('feed', $data);
}
Controller:, (*13)
public function demo() {
$feed = Feeds::make([
'http://blog.case.edu/news/feed.atom',
'http://tutorialslodge.com/feed'
], 5);
$data = array(
'title' => $feed->get_title(),
'permalink' => $feed->get_permalink(),
'items' => $feed->get_items(),
);
return View::make('feed', $data);
}
or Force to read unless content type not a valid RSS, (*14)
public function demo() {
$feed = Feeds::make(['http://blog.case.edu/news/feed.atom',
'http://tutorialslodge.com/feed'
], 5, true); // if RSS Feed has invalid mime types, force to read
$data = array(
'title' => $feed->get_title(),
'permalink' => $feed->get_permalink(),
'items' => $feed->get_items(),
);
return View::make('feed', $data);
}
View:, (*15)
@extends('app')
@section('content')
@foreach ($items as $item)
{{ $item->get_description() }}, (*16)
Posted on {{ $item->get_date('j F Y | g:i a') }}, (*17)
@endforeach
@endsection