Docs Getting Started
Conventions
The conventions described below are NOT mandatory. Feel free to pick what you like and also come up with your own conventions. With that out of the way, here's a list of conventions you may find helpful.
Resource Routes
Laravel Rust supports resource routes, which play really well with Hotwire for most things. A module of handlers named after the actions gets its routes with resource!:
use laravel::prelude::*;
use laravel::routing::resource;
pub fn routes(route: &mut Router) {
resource!(posts);
}
Turbo Laravel relies on these names when a form inside a Turbo Frame fails validation: it sends the user to the form's page instead of back to the page around the frame, from posts::store to posts::create and from posts::update to posts::edit. If you don't use resource routes, at least consider following the naming convention: render forms in handlers named create and edit, and handle them in store and update. Read more in Validation Response.
Partials
You may want to split up your views into smaller chunks (aka. "partials"), such as resources/views/comments/partials/comment.html (the comments.partials.comment view) to render a comment, and reuse it on the page that lists comments and in the Turbo Streams that add one:
{# resources/views/comments/partials/comment.html #}
<div id="{{ dom_id(comment) }}">
<p>{{ comment.body }}</p>
</div>
{# resources/views/posts/show.html #}
<div id="comments">
{% for comment in comments %}
{% include "comments.partials.comment" %}
{% endfor %}
</div>
Model broadcasting uses this convention to find a model's partial: {table}.partials.{morph name}, so comments.partials.comment for a Comment, which renders with the model as comment. You may pick another partial when you need to.
DOM IDs
Give the element that renders a model the model's DOM ID, with the dom_id helper. Turbo Streams target elements by ID, so stream::replace(&comment, …) and stream::remove(&comment) find the comment's element without you spelling its ID out.
The elements new models go into are named after the table: a new comment is appended to #comments.
Channels
A model's broadcast channel is {table}.{route key}, as in posts.42, which Laravel Rust binds to the model in src/routes/channels.rs. Broadcast a model's children, such as a post's comments, on the parent's channel, so a page subscribes once with turbo_stream_from(post).