Folders are a way to request several Stories at once such as getting the latest news articles or a team of people. It wraps Storyblok’s API for retrieving multiple stories. The read()
method will return a collection of Stories matching the specified criteria.
To get a folder of Stories you can use the DefaultFolder
Class in your controller specifying the slug where they are staved in Storyblok, for example.
<?php
namespace App\Http\Controllers;
use App\Storyblok\DefaultFolder;
class NewsController extends Controller
{
public function index() {
$stories = new DefaultFolder();
$stories->slug('news');
return view('storyblok.pages.news', [
'stories' => $stories->read()
]
);
}
}
Call the slug()
method with the path to the content you wish to request from Storyblok. The argument’s value maps to the starts_with
property of the API call so be sure to check the Storyblok documentation.
The sort()
method accepts any valid string as specified in the Storyblok documentation such as content.YOUR_FIELD:asc
or content.YOUR_FIELD:desc
. Read their docs for full details.
If you need more control over your request the settings()
method accepts an array of parameters allowing you to specify any part of the request.
Rather than calling multiple methods each time you need to request a folder you can create custom Folder class by extending \App\Storyblok\DefaultFolder
. Within this class you are free to set any defaults or create methods to fulfill your requirements.
Here is an example that loads Stories from the ‘news’ folder that where published any time before now()
, ordering them by a published_at
datetime field.
<?php
namespace App\Storyblok\Folders;
class News extends \App\Storyblok\DefaultFolder
{
protected $slug = 'news';
protected $sort = 'content.published_at:desc';
public function __construct()
{
$this->settings([
'filter_query' => [
'published_at' => [
'lt-date' => now()->format('Y-m-d h:i')
]
],
'per_page' => 20
]);
}
}
And in your controller you simply need to instantiate your Folder class - no need to call any methods. This handy if you need to reuse a Folder in several locations. Of course you can still override individual settings by calling the above methods if needed.
<?php
namespace App\Http\Controllers;
use App\Storyblok\Folders\News;
class NewsController extends Controller
{
public function index() {
$news = new News();
return view('storyblok.pages.news', [
'news' => $news->read()
]
);
}
}
Here’s a more complete controller that loads the root news
page Story and additional Stories from the news
folder, passing them to the Page’s view.
<?php
namespace App\Http\Controllers;
use App\Storyblok\Folders\News;
use Riclep\Storyblok\StoryblokFacade as StoryBlok;
class NewsController extends Controller
{
public function index() {
$news = new News();
return Storyblok::bySlug('/news')->read()->render(
[
'news' => $news->read()
]
);
}
public function show($slug) {
return Storyblok::bySlug('/news/' . $slug)->read()->render();
}
}