Skip to content

Commit fc79b2c

Browse files
[10.x] Adds documentation about Folio's render function (#8998)
* Adds documentation about Folio's `render` function * Fixes missing removal * Update folio.md * formatting * remove extra words * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7d49b2c commit fc79b2c

File tree

1 file changed

+62
-52
lines changed

1 file changed

+62
-52
lines changed

folio.md

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
- [Nested Routes](#nested-routes)
99
- [Index Routes](#index-routes)
1010
- [Route Parameters](#route-parameters)
11-
- [Named Routes](#named-routes)
1211
- [Route Model Binding](#route-model-binding)
1312
- [Soft Deleted Models](#soft-deleted-models)
13+
- [Render Hooks](#render-hooks)
14+
- [Named Routes](#named-routes)
1415
- [Middleware](#middleware)
15-
- [PHP Blocks](#php-blocks)
1616
- [Route Caching](#route-caching)
1717

1818
<a name="introduction"></a>
@@ -164,33 +164,6 @@ When capturing multiple segments, the captured segments will be injected into th
164164
</ul>
165165
```
166166

167-
<a name="named-routes"></a>
168-
## Named Routes
169-
170-
You may specify a name for a given page's route using the `name` function:
171-
172-
```php
173-
<?php
174-
175-
use function Laravel\Folio\name;
176-
177-
name('users.index');
178-
```
179-
180-
Just like Laravel's named routes, you may use the `route` function to generate URLs to Folio pages that have been assigned a name:
181-
182-
```php
183-
<a href="{{ route('users.index') }}">
184-
All Users
185-
</a>
186-
```
187-
188-
If the page has parameters, you may simply pass their values to the `route` function:
189-
190-
```php
191-
route('users.show', ['user' => $user]);
192-
```
193-
194167
<a name="route-model-binding"></a>
195168
## Route Model Binding
196169

@@ -243,6 +216,66 @@ withTrashed();
243216
</div>
244217
```
245218

219+
<a name="render-hooks"></a>
220+
## Render Hooks
221+
222+
By default, Folio will return the content of the page's Blade template as the response to the incoming request. However, you may customize the response by invoking the `render` function within the page's template.
223+
224+
The `render` function accepts a closure which will receive the `View` instance being rendered by Folio, allowing you to add additional data to the view or customize the entire response. In addition to receiving the `View` instance, any additional route parameters or model bindings will also be provided to the `render` closure:
225+
226+
```php
227+
<?php
228+
229+
use App\Models\Post;
230+
use Illuminate\Support\Facades\Auth;
231+
use Illuminate\View\View;
232+
233+
use function Laravel\Folio\render;
234+
235+
render(function (View $view, Post $post) {
236+
if (! Auth::user()->can('view', $post)) {
237+
return response('Unauthorized', 403);
238+
}
239+
240+
return $view->with('photos', $post->author->photos);
241+
}); ?>
242+
243+
<div>
244+
{{ $post->content }}
245+
</div>
246+
247+
<div>
248+
This author has also taken {{ count($photos) }} photos.
249+
</div>
250+
```
251+
252+
<a name="named-routes"></a>
253+
## Named Routes
254+
255+
You may specify a name for a given page's route using the `name` function:
256+
257+
```php
258+
<?php
259+
260+
use function Laravel\Folio\name;
261+
262+
name('users.index');
263+
```
264+
265+
Just like Laravel's named routes, you may use the `route` function to generate URLs to Folio pages that have been assigned a name:
266+
267+
```php
268+
<a href="{{ route('users.index') }}">
269+
All Users
270+
</a>
271+
```
272+
273+
If the page has parameters, you may simply pass their values to the `route` function:
274+
275+
```php
276+
route('users.show', ['user' => $user]);
277+
```
278+
246279
<a name="middleware"></a>
247280
## Middleware
248281

@@ -300,29 +333,6 @@ Folio::path(resource_path('views/pages'))->middleware([
300333
]);
301334
```
302335

303-
<a name="php-blocks"></a>
304-
## PHP Blocks
305-
306-
When using Folio, the `<?php` and `?>` tags are reserved for the Folio page definition functions such as `middleware` and `withTrashed`.
307-
308-
Therefore, if you need to write PHP code that should be executed within your Blade template, you should use the `@php` Blade directive:
309-
310-
```php
311-
@php
312-
if (! Auth::user()->can('view-posts', $user)) {
313-
abort(403);
314-
}
315-
316-
$posts = $user->posts;
317-
@endphp
318-
319-
@foreach ($posts as $post)
320-
<div>
321-
{{ $post->title }}
322-
</div>
323-
@endforeach
324-
```
325-
326336
<a name="route-caching"></a>
327337
## Route Caching
328338

0 commit comments

Comments
 (0)