Skip to content

Commit

Permalink
documenting various things
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 16, 2017
1 parent d1ba6f8 commit a941878
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 10 deletions.
38 changes: 38 additions & 0 deletions blade.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [Template Inheritance](#template-inheritance)
- [Defining A Layout](#defining-a-layout)
- [Extending A Layout](#extending-a-layout)
- [Components & Slots](#components-and-slots)
- [Displaying Data](#displaying-data)
- [Blade & JavaScript Frameworks](#blade-and-javascript-frameworks)
- [Control Structures](#control-structures)
Expand Down Expand Up @@ -81,6 +82,43 @@ Blade views may be returned from routes using the global `view` helper:
return view('child');
});

<a name="components-and-slots"></a>
## Components & Slots

Components and slots provide similar benefits to sections and layouts; however, some may find the mental model of components and slots easier to understand. First, let's imagine a reusable "alert" component we would like to reuse throughout our application:

<!-- /resources/views/alert.blade.php -->

<div class="alert alert-danger">
{{ $slot }}
</div>

The `{{ $slot }}` variable will contain the content we wish to inject into the component. Now, to construct this component, we can use the `@component` Blade directive:

@component('alert')
<strong>Whoops!</strong> Something went wrong!
@endcomponent

Sometimes it is helpful to define multiple slots for a component. Let's modify our alert component to allow for the injection of a "title". Named slots may be displayed by simply "echoing" the variable that matches their name:

<!-- /resources/views/alert.blade.php -->

<div class="alert alert-danger">
<div class="alert-title">{{ $title }}</div>

{{ $slot }}
</div>

Now, we can inject content into the named slot using the `@slot` directive. Any content is not within a `@slot` directive will be passed to the component in the `$slot` variable:

@component('alert')
@slot('title')
Forbidden
@endslot

You are not allowed to access this resource!
@endcomponent

<a name="displaying-data"></a>
## Displaying Data

Expand Down
31 changes: 23 additions & 8 deletions broadcasting.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ When a user is viewing one of their orders, we don't want them to have to refres

class ShippingStatusUpdated implements ShouldBroadcast
{
//
/**
* Information about the shipping status update.
*
* @var string
*/
public $update;
}

The `ShouldBroadcast` interface requires our event to define a `broadcastOn` method. This method is responsible for returning the channels that the event should broadcast on. An empty stub of this method is already defined on generated event classes, so we only need to fill in its details. We only want the creator of the order to be able to view status updates, so we will broadcast the event on a private channel that is tied to the order:
Expand All @@ -155,15 +160,15 @@ The `ShouldBroadcast` interface requires our event to define a `broadcastOn` met

#### Authorizing Channels

Remember, users must be authorized to listen on private channels. We may define our channel authorization rules in the `boot` method of the `BroadcastServiceProvider`. In this example, we need to verify that any user attempting to listen on the private `order.1` channel is actually the creator of the order:
Remember, users must be authorized to listen on private channels. We may define our channel authorization rules in the `routes/channels.php` file. In this example, we need to verify that any user attempting to listen on the private `order.1` channel is actually the creator of the order:

Broadcast::channel('order.*', function ($user, $orderId) {
Broadcast::channel('order.{orderId}', function ($user, $orderId) {
return $user->id === Order::findOrNew($orderId)->user_id;
});

The `channel` method accepts two arguments: the name of the channel and a callback which returns `true` or `false` indicating whether the user is authorized to listen on the channel.

All authorization callbacks receive the currently authenticated user as their first argument and any additional wildcard parameters as their subsequent arguments. In this example, we are using the `*` character to indicate that the "ID" portion of the channel name is a wildcard.
All authorization callbacks receive the currently authenticated user as their first argument and any additional wildcard parameters as their subsequent arguments. In this example, we are using the `{orderId}` placeholder to indicate that the "ID" portion of the channel name is a wildcard.

#### Listening For Event Broadcasts

Expand Down Expand Up @@ -292,15 +297,25 @@ The `Broadcast::routes` method will automatically place its routes within the `w
<a name="defining-authorization-callbacks"></a>
### Defining Authorization Callbacks

Next, we need to define the logic that will actually perform the channel authorization. Like defining the authorization routes, this is also done in the `boot` method of the `BroadcastServiceProvider`. In this method, you may use the `Broadcast::channel` method to register channel authorization callbacks:
Next, we need to define the logic that will actually perform the channel authorization. This is done in the `routes/channels.php` file that is included with your application. In this file, you may use the `Broadcast::channel` method to register channel authorization callbacks:

Broadcast::channel('order.*', function ($user, $orderId) {
Broadcast::channel('order.{orderId}', function ($user, $orderId) {
return $user->id === Order::findOrNew($orderId)->user_id;
});

The `channel` method accepts two arguments: the name of the channel and a callback which returns `true` or `false` indicating whether the user is authorized to listen on the channel.

All authorization callbacks receive the currently authenticated user as their first argument and any additional wildcard parameters as their subsequent arguments. In this example, we are using the `*` character to indicate that the "ID" portion of the channel name is a wildcard.
All authorization callbacks receive the currently authenticated user as their first argument and any additional wildcard parameters as their subsequent arguments. In this example, we are using the `{orderId}` placeholder to indicate that the "ID" portion of the channel name is a wildcard.

#### Authorization Callback Model Binding

Just like HTTP routes, channel routes may also take advantage of implicit and explicit [route model binding](/docs/{{version}}/routing#route-model-binding). For example, instead of receiving the string or numeric order ID, I may request an actual `Order` model instance:

use App\Order;

Broadcast::channel('order.{order}', function ($user, Order $order) {
return $user->id === $order->user_id;
});

<a name="broadcasting-events"></a>
## Broadcasting Events
Expand Down Expand Up @@ -488,4 +503,4 @@ Once you have configured a notification to use the broadcast channel, you may li
console.log(notification.type);
});

In this example, all notifications sent to `App\User` instances via the `broadcast` channel would be received by the callback. A channel authorization callback for the `App.User.*` channel is included in the default `BroadcastServiceProvider` that ships with the Laravel framework.
In this example, all notifications sent to `App\User` instances via the `broadcast` channel would be received by the callback. A channel authorization callback for the `App.User.{id}` channel is included in the default `BroadcastServiceProvider` that ships with the Laravel framework.
18 changes: 18 additions & 0 deletions collections.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [Introduction](#introduction)
- [Creating Collections](#creating-collections)
- [Available Methods](#available-methods)
- [Higher Order Messages](#higher-order-messages)

<a name="introduction"></a>
## Introduction
Expand Down Expand Up @@ -1433,3 +1434,20 @@ The `zip` method merges together the values of the given array with the values o
$zipped->all();

// [['Chair', 100], ['Desk', 200]]

<a name="higher-order-messages"></a>
## Higher Order Messages

Collections also provide support for "higher order messages", which are short-cuts for performing common actions on collections. The collection methods that provide higher order messages are: `contains`, `each`, `every`, `filter`, `first`, `map`, `partition`, `reject`, `sortBy`, `sortByDesc`, and `sum`.

Each higher order message can be accessed as a dynamic property on a collection instance. For instance, let's use the `each` higher order message to call a method on each object within a collection:

$users = User::where('votes', '>', 500)->get();

$users->each->markAsVip();

Likewise, we can use the `sum` higher order message to gather the total number of "votes" for a collection of users:

$users = User::where('group', 'Development')->get();

return $users->sum->votes;
10 changes: 10 additions & 0 deletions helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Laravel includes a variety of global "helper" PHP functions. Many of these funct
[redirect](#method-redirect)
[request](#method-request)
[response](#method-response)
[retry](#method-retry)
[session](#method-session)
[value](#method-value)
[view](#method-view)
Expand Down Expand Up @@ -965,6 +966,15 @@ The `response` function creates a [response](/docs/{{version}}/responses) instan

return response()->json(['foo' => 'bar'], 200, $headers);

<a name="method-retry"></a>
#### `retry()` {#collection-method}

The `retry` function attempts to execute the given callback until the given maximum attempt threshold is met. If the callback does not throw an exception, it's return value will be returned. If the callback throws an exception, it will automatically be retried. If the maximum attempt count is exceeded, the exception will be thrown:

return retry(5, function () {
// Attempt 5 times while resting 100ms in between attempts...
}, 100);

<a name="method-session"></a>
#### `session()` {#collection-method}

Expand Down
2 changes: 1 addition & 1 deletion localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ After defining a language line that has pluralization options, you may use the `

Since the Laravel translator is powered by the Symfony Translation component, you may create even more complex pluralization rules which specify language lines for multiple number ranges:

'apples' => '{0} There are none|[1,19] There are some|[20,Inf] There are many',
'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many',

<a name="overriding-package-language-files"></a>
## Overriding Package Language Files
Expand Down
50 changes: 49 additions & 1 deletion queues.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [Dispatching Jobs](#dispatching-jobs)
- [Delayed Dispatching](#delayed-dispatching)
- [Customizing The Queue & Connection](#customizing-the-queue-and-connection)
- [Specifying Max Job Attempts / Timeout Values](#max-job-attempts-and-timeout)
- [Error Handling](#error-handling)
- [Running The Queue Worker](#running-the-queue-worker)
- [Queue Priorities](#queue-priorities)
Expand Down Expand Up @@ -277,10 +278,57 @@ Of course, you may chain the `onConnection` and `onQueue` methods to specify the
->onConnection('sqs')
->onQueue('processing');

<a name="max-job-attempts-and-timeout"></a>
### Specifying Max Job Attempts / Timeout Values

#### Max Attempts

One approach to specifying the maximum number of times a job may be attempted is via the `--tries` switch on the Artisan command line:

php artisan queue:work --tries=3

However, you may take a more granular approach by defining the maximum number of attempts on the job class itself. If the maximum number of attempts is specified on the job, it will take precedence over the value provided on the command line:

<?php

namespace App\Jobs;

class ProcessPodcast implements ShouldQueue
{
/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 5;
}

#### Timeout

Likewise, the maximum number of seconds that jobs can run may be specified using the `--timeout` switch on the Artisan command line:

php artisan queue:work --timeout=30

However, you may also define the maximum number of seconds a job should be allowed to run on the job class itself. If the timeout is specified on the job, it will take precedence over any timeout specified on the command line:

<?php

namespace App\Jobs;

class ProcessPodcast implements ShouldQueue
{
/**
* The number of seconds the job can run before timing out.
*
* @var int
*/
public $timeout = 120;
}

<a name="error-handling"></a>
### Error Handling

If an exception is thrown while the job is being processed, the job will automatically be released back onto the queue so it may be attempted again. The job will continue to be released until it has been attempted the maximum number of times allowed by your application. The number of maximum attempts is defined by the `--tries` switch used on the `queue:work` Artisan command. More information on running the queue worker [can be found below](#running-the-queue-worker).
If an exception is thrown while the job is being processed, the job will automatically be released back onto the queue so it may be attempted again. The job will continue to be released until it has been attempted the maximum number of times allowed by your application. The maximum number of attempts is defined by the `--tries` switch used on the `queue:work` Artisan command. Alternatively, the maximum number of attempts may be defined on the job class itself. More information on running the queue worker [can be found below](#running-the-queue-worker).

<a name="running-the-queue-worker"></a>
## Running The Queue Worker
Expand Down

0 comments on commit a941878

Please sign in to comment.