|
8 | 8 | - [Nested Routes](#nested-routes)
|
9 | 9 | - [Index Routes](#index-routes)
|
10 | 10 | - [Route Parameters](#route-parameters)
|
11 |
| -- [Named Routes](#named-routes) |
12 | 11 | - [Route Model Binding](#route-model-binding)
|
13 | 12 | - [Soft Deleted Models](#soft-deleted-models)
|
| 13 | +- [Render Hooks](#render-hooks) |
| 14 | +- [Named Routes](#named-routes) |
14 | 15 | - [Middleware](#middleware)
|
15 |
| -- [PHP Blocks](#php-blocks) |
16 | 16 | - [Route Caching](#route-caching)
|
17 | 17 |
|
18 | 18 | <a name="introduction"></a>
|
@@ -164,33 +164,6 @@ When capturing multiple segments, the captured segments will be injected into th
|
164 | 164 | </ul>
|
165 | 165 | ```
|
166 | 166 |
|
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 |
| - |
194 | 167 | <a name="route-model-binding"></a>
|
195 | 168 | ## Route Model Binding
|
196 | 169 |
|
@@ -243,6 +216,66 @@ withTrashed();
|
243 | 216 | </div>
|
244 | 217 | ```
|
245 | 218 |
|
| 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 | + |
246 | 279 | <a name="middleware"></a>
|
247 | 280 | ## Middleware
|
248 | 281 |
|
@@ -300,29 +333,6 @@ Folio::path(resource_path('views/pages'))->middleware([
|
300 | 333 | ]);
|
301 | 334 | ```
|
302 | 335 |
|
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 |
| - |
326 | 336 | <a name="route-caching"></a>
|
327 | 337 | ## Route Caching
|
328 | 338 |
|
|
0 commit comments