Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tonysm committed Jan 14, 2025
1 parent 7ddefd6 commit d9ca974
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 50 deletions.
6 changes: 2 additions & 4 deletions docs/blade-helpers.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
extends: _layouts.v1-docs
title: Blade Directives
description: Blade Directives
title: Blade Helpers
description: Blade Directives and Components
order: 4
---

Expand Down Expand Up @@ -46,5 +46,3 @@ To the `:id` prop, you may pass a string, which will be used as-is as the DOM ID
```

Additionally, you may also pass along any prop that is supported by the Turbo Frame custom Element to the `<x-turbo-frame>` Blade component, like `target`, `src`, or `loading`. These are the listed attributes, but any other attribute will also be forwarded to the `<turbo-frame>` tag that will be rendered by the `<x-turbo-frame>` component. For a full list of what's possible to do with Turbo Frames, see the [documentation](https://turbo.hotwired.dev/handbook/frames).

[Continue to Helper Functions...](/docs/{{version}}/helper-functions)
16 changes: 7 additions & 9 deletions docs/broadcasting.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Broadcasting Turbo Streams
order: 7
---

# Broadcasting Turbo Streams Over WebSockets With Laravel Echo
# Broadcasting Turbo Streams

So far, we have used Turbo Streams over HTTP to handle the case of updating multiple parts of the page for a single user after a form submission. In addition to that, you may want to broadcast model changes over WebSockets to all users that are viewing the same page. Although nice, **you don't have to use WebSockets if you don't have the need for it. You may still benefit from Turbo Streams over HTTP.**

Expand Down Expand Up @@ -179,7 +179,7 @@ class Comment extends Model

This will also automatically hook into the model events, but instead of broadcasting new instances as `append` it will use `prepend`.

Secondly, it will send all changes to this model's broadacsting channel, except for when the model is created (since no one would be listening to its direct channel). In our case, we want to direct the broadcasts to the post linked to this model instead. We can achieve that by adding a `$broadcastsTo` property to the model, like so:
Secondly, it will send all changes to this model's broadcasting channel, except for when the model is created (since no one would be listening to its direct channel). In our case, we want to direct the broadcasts to the post linked to this model instead. We can achieve that by adding a `$broadcastsTo` property to the model, like so:

```php
class Comment extends Model
Expand Down Expand Up @@ -230,7 +230,7 @@ class Comment extends Model
}
```

Newly created models using the auto-broadcasting feature will be broadcasted to a pluralized version of the model's basename. So if you have a `App\Models\PostComment`, you may expect broadcasts of newly created models to be sent to a private channel called `post_comments`. Again, this convention is only valid for newly created models. Updates/Removals will still be sent to the model's own private channel by default using Laravel's convention for channel names. You may want to specify the channel name for newly created models to be broadcasted to with the `stream` key:
Newly created models using the auto-broadcasting feature will be broadcasted to a pluralized version of the model's basename. So if you have a `App\Models\PostComment`, you may expect broadcasts of newly created models to be sent to a private channel called `post_comments`. Again, this convention is only valid for newly created models. Updates/Removals will still be sent to the model's own private channel by default using Laravel's convention for channel names. You may want to specify the channel name for newly created models to be broadcast to with the `stream` key:

```php
class Comment extends Model
Expand All @@ -247,7 +247,7 @@ Having a `$broadcastsTo` property or implementing the `broadcastsTo()` method in

## Broadcasting Turbo Streams to Other Users Only

As mentioned erlier, you may want to feed the current user with Turbo Streams using HTTP requests and only send the broadcasts to other users. There are a couple ways you can achieve that.
As mentioned earlier, you may want to feed the current user with Turbo Streams using HTTP requests and only send the broadcasts to other users. There are a couple ways you can achieve that.

First, you can chain on the broadcasting methods, like so:

Expand All @@ -268,7 +268,7 @@ Turbo::broadcastToOthers(function () {

This way, any broadcast that happens inside the scope of the Closure will only be sent to other users.

Third, you may use that same method but without the Closure inside a ServiceProvider, for instance, to instruct the package to only send turbo stream broadcasts to other users globally:
Third, you may use that same method but without the Closure inside a `ServiceProvider`, for instance, to instruct the package to only send turbo stream broadcasts to other users globally:

```php
<?php
Expand Down Expand Up @@ -366,7 +366,7 @@ TurboStream::broadcastAction('scroll_to', target: 'todo_123');

## Handmade Broadcasting Using The `turbo_stream()` Response Builder

Alternatively to use the `TurboStream` Facade (or Factory type-hint), you may also broadcast directly from the `turbo_stream()` function response builder:
Alternatively to using the `TurboStream` Facade (or Factory type-hint), you may also broadcast directly from the `turbo_stream()` function response builder:

```php
turbo_stream()
Expand All @@ -389,7 +389,7 @@ turbo_stream($comment)
->broadcastTo($comment->post, fn ($broadcast) => $broadcast->toOthers());
```

Similarly to using the Facade, you may also want to broadcast to private or presence string channels like so:
Similar to using the Facade, you may also want to broadcast to private or presence string channels like so:

```php
// To private channels...
Expand All @@ -402,5 +402,3 @@ turbo_stream()
->append('notifications', 'Hello World')
->broadcastToPresenceChannel('chat.123', fn ($broadcast) => $broadcast->toOthers());
```

[Continue to Livewire Integration...](/docs/{{version}}/livewire)
14 changes: 6 additions & 8 deletions docs/conventions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
extends: _layouts.v1-docs
title: Convetions
title: Conventions
description: All the (optional) conventions and recommendations
order: 3
---
Expand All @@ -15,7 +15,7 @@ Laravel supports [resource routes](https://laravel.com/docs/controllers#resource

If you don't want to use resource routes, at least consider using the naming convention: render forms in a route name ending in `.create` or `.edit` and name their handler routes ending with `.store` or `.update`.

You may want to define exceptions to the route guessing behavior. In that case, you may want to set the `redirect_guessing_exceptions` in the `config/turbo-laravel.php` config file:
You may define exceptions to the route guessing behavior by setting the `redirect_guessing_exceptions` in the `config/turbo-laravel.php` config file:

```php
return [
Expand All @@ -26,16 +26,14 @@ return [
];
```

When using this config, the redirection behavior will still happen, but the package will not guess routes. See the [Validation Response Redirects](/docs/{{version}}/validation-response-redirects) page to know more about why this happens.
When using this config, the redirection behavior will still happen, but the package will not guess routes. See the [Validation Response Redirects](/1.x/docs/validation-response-redirects) page to know more about why this happens.

## Partials

You may want to split up your views in smaller chunks (aka. "partials"), such as `comments/_comment.blade.php` which displays a comment resource, or `comments/_form.blade.php` for the form to either create/update comments. This will allow you to reuse these _partials_ in [Turbo Streams](/docs/{{version}}/turbo-streams).
You may want to split up your views in smaller chunks (aka. "partials"), such as `comments/_comment.blade.php` which displays a comment resource, or `comments/_form.blade.php` for the form to either create/update comments. This will allow you to reuse these _partials_ in [Turbo Streams](/1.x/docs/turbo-streams).

The models' partials (such as the `comments/_comment.blade.php` for a `Comment` model) may only rely on having a single `$comment` instance variable passed to them. That's because the package will, by default, figure out the partial for a given model when broadcasting and will also pass the model to such partial, using the class basename as the variable instance in _camelCase_. Again, that's by default, you can customize most things. Read the [Broadcasting](/docs/{{version}}/broadcasting) section to know more.
The models' partials (such as the `comments/_comment.blade.php` for a `Comment` model) may only rely on having a single `$comment` instance variable passed to them. That's because the package will, by default, figure out the partial for a given model when broadcasting and will also pass the model to such partial, using the class basename as the variable instance in _camelCase_. Again, that's by default, you can customize most things. Read the [Broadcasting](/1.x/docs/broadcasting) section to know more.

## Turbo Stream Channel Names

You may use the model's Fully Qualified Class Name (aka. FQCN) as your Broadcasting Channel authorization routes with a wildcard, such as `App.Models.Comment.{comment}` for a `Comment` model living in `App\\Models\\` - the wildcard's name doesn't matter, as long as there is one. This is the default [broadcasting channel naming convention](https://laravel.com/docs/8.x/broadcasting#model-broadcasting-conventions) in Laravel.

[Continue to Blade Helpers...](/docs/{{version}}/blade-helpers)
You may use the model's Fully Qualified Class Name (aka. FQCN) as your Broadcasting Channel authorization routes with a wildcard, such as `App.Models.Comment.{comment}` for a `Comment` model living in `App\\Models\\` - the wildcard's name doesn't matter, as long as there is one. This is the default [broadcasting channel naming convention](https://laravel.com/docs/broadcasting#model-broadcasting-conventions) in Laravel.
2 changes: 0 additions & 2 deletions docs/csrf.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,3 @@ Since Turbo.js intercepts form submissions and converts those to fetch requests
```

With that being said, you may still want to use the `@csrf` Blade directive if you want to support users with JavaScript disabled, since the forms will still work if they contain the CSRF token.

[Continue to Turbo Native...](/docs/{{version}}/turbo-native)
6 changes: 1 addition & 5 deletions docs/helper-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dom_class($comment);

This function will generate the DOM class named based on your model's classname. If you have an instance of a `App\Models\Comment` model, it will generate a `comment` DOM class.

Similarly to the `dom_id()` function, you may also pass a context prefix as the second parameter:
Similar to the `dom_id()` function, you may also pass a context prefix as the second parameter:

```php
dom_class($comment, 'reactions_list');
Expand Down Expand Up @@ -67,8 +67,4 @@ return turbo_stream_view('comments.turbo.created', [
]);
```

---

All these functions are also registered globally, so you may use it directly without the `use` statements (this is useful in contexts like Blade views, for instance).

[Continue to Turbo Streams...](/docs/{{version}}/turbo-streams)
2 changes: 0 additions & 2 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,3 @@ php artisan turbo:install --alpine
```

_Note: the `--jet` option also adds all the necessary Alpine dependencies since Jetstream depends on Alpine._

[Continue to Overview...](/docs/{{version}}/overview)
2 changes: 1 addition & 1 deletion docs/known-issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ If you ever encounter an issue with the package, look here first for documented

## Fixing Laravel's Previous URL Issue

Visits from Turbo Frames will hit your application and Laravel by default keeps track of previously visited URLs to be used with helpers like `url()->previous()`, for instance. This might be confusing because chances are that you wouldn't want to redirect users to the URL of the most recent Turbo Frame that hit your app. So, to avoid storying Turbo Frames visits as Laravel's previous URL, head to the [issue](https://github.com/tonysm/turbo-laravel/issues/60#issuecomment-1123142591) where a solution was discussed.
Visits from Turbo Frames will hit your application and Laravel by default keeps track of previously visited URLs to be used with helpers like `url()->previous()`, for instance. This might be confusing because chances are that you wouldn't want to redirect users to the URL of the most recent Turbo Frame that hit your app. So, to avoid storing Turbo Frames visits as Laravel's previous URL, head to the [issue](https://github.com/tonysm/turbo-laravel/issues/60#issuecomment-1123142591) where a solution was discussed.
2 changes: 0 additions & 2 deletions docs/livewire.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,3 @@ Now, we can use this element in a page where we want to have the integration bet
That's it! With that, we got Livewire to generate Turbo Streams, dispatch it as a browser event, which gets intercepted by our custom HTML element and applied to the page!

This is only an example of what a deeper integration could look like.

[Continue to Validation Response Redirects...](/docs/{{version}}/validation-response-redirects)
2 changes: 0 additions & 2 deletions docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,3 @@ As mentioned earlier, you may also trigger a Turbo Frame with forms and links th
You could also "hide" this link and trigger a "click" event with JavaScript programmatically to trigger the Turbo Frame to reload, for example.

So far, all vanilla Hotwire and Turbo.

[Continue to Conventions...](/docs/{{version}}/conventions)
4 changes: 1 addition & 3 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,4 @@ class CreatesCommentsTest extends TestCase
}
```

*Note: If you're using the automatic model changes broadcasting, make sure your `turbo-laravel.queue` config key is set to false, otherwise actions may not be dispatched during test because the model observer only fires them after the transaction is commited, which never happens in tests since they run inside a transaction.*

[Continue to Known Issues...](/docs/{{version}}/known-issues)
*Note: If you're using the automatic model changes broadcasting, make sure your `turbo-laravel.queue` config key is set to false, otherwise actions may not be dispatched during test because the model observer only fires them after the transaction is committed, which never happens in tests since they run inside a transaction.*
2 changes: 0 additions & 2 deletions docs/turbo-native.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,3 @@ return [
],
];
```

[Continue to Testing...](/docs/{{version}}/testing)
12 changes: 5 additions & 7 deletions docs/turbo-streams.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
extends: _layouts.v1-docs
title: Turbo Streams
description: The Turbo Streams Components and Helpers
description: The Turbo Streams Components and Helpers
order: 6
---

Expand Down Expand Up @@ -57,7 +57,7 @@ Here's what the HTML response will look like:
</turbo-stream>
```

Most of these things were "guessed" based on the [naming conventions](/docs/{{version}}/conventions) we talked about earlier. But you can override most things, like so:
Most of these things were "guessed" based on the [naming conventions](/1.x/docs/conventions) we talked about earlier. But you can override most things, like so:

```php
return turbo_stream($comment)->target('post_comments');
Expand Down Expand Up @@ -94,7 +94,7 @@ turbo_stream()->remove($comment);

For these shorthand stream builders, you may pass an instance of an Eloquent model, which will be used to figure out things like `target`, `action`, and the `view` partial as well as the view data passed to them.

Alternativelly, you may also pass strings to the shorthand stream builders, which will be used as the target, and an optional content string, which will be rendered instead of a partial, for instance:
Alternatively, you may also pass strings to the shorthand stream builders, which will be used as the target, and an optional content string, which will be rendered instead of a partial, for instance:

```php
turbo_stream()->append('statuses', __('Comment was successfully created!'));
Expand Down Expand Up @@ -249,7 +249,7 @@ class ChirpsController extends Controller
turbo_stream()->append('notifications', view('layouts.notification', [
'message' => __('Chirp deleted.'),
])),
turbo_stream()->flash(__('Chirp deleted.')), // [tl! remove:-3,3 add]
turbo_stream()->flash(__('Chirp deleted.')),
]);
}

Expand Down Expand Up @@ -316,7 +316,7 @@ Remember, these are Blade views, so you have the full power of Blade at your han
@endif
```

Similar to the `<x-turbo-frame>` Blade component, there's also a `<x-turbo-stream>` Blade component that can simplify things a bit. It has the same convention of figureing out the DOM ID when you're passing a model instance or an array as the `<x-turbo-frame>` component applied to the `target` attribute. When using the component version, there's also no need to specify the template wrapper for the Turbo Stream tag, as that will be added by the component itself. So, the same example would look something like this:
Similar to the `<x-turbo-frame>` Blade component, there's also a `<x-turbo-stream>` Blade component that can simplify things a bit. It has the same convention of figuring out the DOM ID when you're passing a model instance or an array as the `<x-turbo-frame>` component applied to the `target` attribute. When using the component version, there's also no need to specify the template wrapper for the Turbo Stream tag, as that will be added by the component itself. So, the same example would look something like this:

```blade
@include('layouts.turbo.flash_stream')
Expand Down Expand Up @@ -347,5 +347,3 @@ Turbo.StreamActions.console_log = function () {
```

Custom actions are only supported from Blade views. You cannot return those from controllers using the Pending Streams Builder, for instance.

[Continue to Broadcasting...](/docs/{{version}}/broadcasting)
4 changes: 1 addition & 3 deletions docs/validation-response-redirects.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Examples:

If a guessed route name doesn't exist (which will always happen if you don't use the route resource convention), the middleware will not change the default handling of validation errors.

When you're not using the [resource route naming convention](/docs/{{version}}/conventions), you can override redirect behavior by catching the `ValidationException` yourself and re-throwing it overriding the redirect with the `redirectTo` method. If the exception has that, the middleware will respect it and make a GET request to that location instead of trying to guess it.
When you're not using the [resource route naming convention](/1.x/docs/conventions), you can override redirect behavior by catching the `ValidationException` yourself and re-throwing it overriding the redirect with the `redirectTo` method. If the exception has that, the middleware will respect it and make a GET request to that location instead of trying to guess it.

Here's how you may set the `redirectTo` property:

Expand Down Expand Up @@ -82,5 +82,3 @@ class Kernel extends HttpKernel
];
}
```

[Continue to CSRF Protection...](/docs/{{version}}/csrf)

0 comments on commit d9ca974

Please sign in to comment.