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
php artisan make:storyThe command asks for the class name, then What will this story describe?
| Choice | Laravel Analogy | What the Class Contains |
|---|---|---|
| One activity, published with its data | like an event | constructor data and toFeedActivity() |
| Every activity for one model | like a resource controller | one declaration method per verb |
| A single verb | like a single action controller | that 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:
php artisan make:story OrderStory --model=Order<?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:
use App\Models\Order;
use App\Stories\OrderStory;
use Storyfeed\Facades\Story;
Story::for(Order::class)->verb('place', [OrderStory::class, 'place']);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:
<?php
namespace App\Stories;
class TicketStory
{
public function reopen(): string
{
return ':actor reopened :object';
}
}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
php artisan make:story PlaceStory --invokable --verb=place --object=Order<?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:
use App\Models\Order;
use App\Stories\PlaceStory;
use Storyfeed\Facades\Story;
Story::for(Order::class)->verb('place', PlaceStory::class);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
use App\Models\Order;
use App\Stories\OrderStory;
use Storyfeed\Facades\Story;
Story::resource(Order::class, OrderStory::class);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:
| Method | Stored 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 Type | The Method Returns |
|---|---|
Storyfeed\Stories\Verb | the supplied definition with its options set |
string | the 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:
// 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:
use App\Models\Order;
use App\Stories\OrderStory;
use Storyfeed\Facades\Story;
Story::resource(Order::class, OrderStory::class)->only('place', 'complete');use App\Models\Order;
use App\Stories\OrderStory;
use Storyfeed\Facades\Story;
Story::resource(Order::class, OrderStory::class)->except('restore');Choose one resource registration. Excluded verbs also lose their resource names.
Registering Multiple Resources
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']]);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
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);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
php artisan make:story --from-doctorThe 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.