Pre-1.0. Not intended for public use yet. The API is subject to undocumented change, and these pages may already be wrong about it.

Skip to content

Story Classes ​

Introduction ​

A Story class groups a model's verb definitions in one file. Each public verb method defines what an activity says in the feed, in place of an inline definition in routes/feed.php.

Generating Story Classes ​

bash
php artisan make:story

The command asks for the class name, then What will this story describe?

ChoiceLaravel AnalogyWhat the Class Contains
One activity, published with its datalike an eventconstructor data and toFeedActivity()
Every activity for one modellike a resource controllerone declaration method per verb
A single verblike a single action controllerthat verb's headlines in their own class

Each choice prints the routes/feed.php line to add. See Commands for all options.

Select and uncomment a generated headline before compiling the definitions.

Writing Story Classes ​

Basic Story Classes ​

Generate a Story class for the Order model and define its verbs:

shell
php artisan make:story OrderStory --model=Order
app/Stories/OrderStory.php
php
<?php

namespace App\Stories;

use Storyfeed\Stories\Verb;

class OrderStory
{
    public function place(Verb $verb): Verb
    {
        return $verb
            ->headline(':actor placed :object with :target')
            ->icon('shopping-bag');
    }

    public function complete(): string
    {
        return ':actor completed :object';
    }
}

A Story class needs no base class. Bind one public method to a verb in the feed file:

routes/feed.php
php
use App\Models\Order;
use App\Stories\OrderStory;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('place', [OrderStory::class, 'place']);
Erica Sinclair

Only place is registered here. Bind complete separately when you need it. Use explicit method bindings to select individual verbs from a class. Each method returns the supplied Verb or a headline string.

The stored verb can differ from the method name:

app/Stories/TicketStory.php
php
<?php

namespace App\Stories;

class TicketStory
{
    public function reopen(): string
    {
        return ':actor reopened :object';
    }
}
routes/feed.php
php
use App\Models\Ticket;
use App\Stories\TicketStory;
use Storyfeed\Facades\Story;

Story::for(Ticket::class)->verb('re-open', [TicketStory::class, 'reopen']);

The binding keeps re-open as the stored verb. To apply a method to every object type, use Story::verb('place', [OrderStory::class, 'place']). Publish with the activity builder; the class defines what those activities say.

Single-Verb Stories ​

Generating an Invokable Story ​

shell
php artisan make:story PlaceStory --invokable --verb=place --object=Order
app/Stories/PlaceStory.php
php
<?php

namespace App\Stories;

use Storyfeed\Stories\Verb;

class PlaceStory
{
    public function __invoke(Verb $verb): Verb
    {
        return $verb
            ->headline(':actor placed :object with :target')
            ->icon('shopping-bag');
    }
}

Registering an Invokable Story ​

Register the invokable class without a method name:

routes/feed.php
php
use App\Models\Order;
use App\Stories\PlaceStory;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('place', PlaceStory::class);
Erica Sinclair

Publish the verb with the activity builder. An invokable declaration may return a Verb or headline string. Registering it with Story::verb('place', PlaceStory::class) applies it to all object types, so its headline must describe each supported type.

Package authors can register Story classes in a service provider.

Story Middleware ​

Attach middleware to a verb binding or a group in routes/feed.php. See Story Middleware for registration, exclusions, and batching.

Resource Stories ​

routes/feed.php
php
use App\Models\Order;
use App\Stories\OrderStory;
use Storyfeed\Facades\Story;

Story::resource(Order::class, OrderStory::class);
Erica Sinclair

A resource registration wires the class's public verb methods in one call. It also defines create, update, delete, and restore; see Conventional Verbs for their headlines and icons. A method with one of those names replaces its complete default definition. Use this registration in place of the individual method binding.

Define verbs in the class and publish activities with the activity builder.

Verbs and Return Types ​

The method name becomes the stored verb in snake_case:

MethodStored Verb
pay()pay
store()store
confirmPayment()confirm_payment
markAsPaid()mark_as_paid

PHP reserved words such as default, print, match, and list are valid method names.

No other name conversion applies: store() records store, and create() records create.

Each method declares its return type:

Return TypeThe Method Returns
Storyfeed\Stories\Verbthe supplied definition with its options set
stringthe headline

Use Verb when setting several options, or string for a headline alone. Keep helpers protected or private, because public methods declare verbs.

Keep a verb's method while stored activities still use it. Storyfeed resolves headlines from the current definitions when retrieving the feed, so removing the method leaves those activities without a headline:

app/Stories/OrderStory.phpprint()
php
// Nothing publishes `print` any more; old rows still read.
public function print(): string
{
    return ':actor printed :object';
}

The supplied Verb supports every definition method, including missingHeadline() for deleted objects. See Deleted Models.

Selecting Verbs ​

Use only() or except() to filter the class methods and conventional verbs by stored verb name:

routes/feed.php
php
use App\Models\Order;
use App\Stories\OrderStory;
use Storyfeed\Facades\Story;

Story::resource(Order::class, OrderStory::class)->only('place', 'complete');
php
use App\Models\Order;
use App\Stories\OrderStory;
use Storyfeed\Facades\Story;

Story::resource(Order::class, OrderStory::class)->except('restore');
Erica Sinclair

Choose one resource registration. Excluded verbs also lose their resource names.

Registering Multiple Resources ​

routes/feed.php
php
use App\Models\MenuItem;
use App\Models\Order;
use App\Stories\OrderStory;
use Storyfeed\Facades\Story;

Story::resources([
    Order::class => OrderStory::class,
    MenuItem::class => null,
], ['except' => ['restore']]);
Erica Sinclair

The Story::resources method registers several models with shared options, as Route::resources does. A null class defines the four conventional verbs. The options accept only and except. Use this example in place of individual resource registrations. To share middleware or role constraints, wrap it in a group.

Supplementing Resource Stories ​

routes/feed.php
php
use App\Models\Order;
use App\Stories\OrderStory;
use App\Stories\PlaceStory;
use Storyfeed\Facades\Story;

Story::resource(Order::class, OrderStory::class);
Story::for(Order::class)->verb('place', PlaceStory::class);
Erica Sinclair

A later explicit method or invokable binding replaces the complete definition for that object type and verb, including its name and middleware. Other resource verbs keep their definitions. Use this to replace one resource method with a dedicated single-verb class.

Dependency Injection ​

Storyfeed resolves Story classes through Laravel's service container. Type-hint dependencies on a verb method to receive them, including Illuminate\Http\Request. Explicit method bindings, invokable classes, and resource methods use the same injection rules.

A verb method can choose its actor from the request. See Request-Based Actors.

Publishing Story Classes ​

A Story class can also build one activity with its data and publish it, as a notification is sent. See Publishing Story Classes.

Generating From Existing Activities ​

bash
php artisan make:story --from-doctor

The command generates an activity class for each recorded type and verb without a headline, using a name such as OrderWasPlaced. It prompts for uncertain past tenses. Without an interactive terminal, it skips those pairs and prints commands for the possible spellings. Run the command with the correct spelling.

Listing and Caching Stories ​

New resource methods appear once definitions compile again: see Listing Definitions and Caching Definitions.

Released under the MIT License.