Skip to content

[11.x] Creation/Definition/Usage and Installation/Configuration code block tabs #9974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 60 additions & 50 deletions artisan.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,49 +111,55 @@ In addition to the commands provided with Artisan, you may build your own custom

To create a new command, you may use the `make:command` Artisan command. This command will create a new command class in the `app/Console/Commands` directory. Don't worry if this directory does not exist in your application - it will be created the first time you run the `make:command` Artisan command:

```shell
```shell tab=Creation
php artisan make:command SendEmails
```

<a name="command-structure"></a>
### Command Structure

After generating your command, you should define appropriate values for the `signature` and `description` properties of the class. These properties will be used when displaying your command on the `list` screen. The `signature` property also allows you to define [your command's input expectations](#defining-input-expectations). The `handle` method will be called when your command is executed. You may place your command logic in this method.
```php tab=Definition filename=app/Console/Commands/SendEmails.php
<?php

Let's take a look at an example command. Note that we are able to request any dependencies we need via the command's `handle` method. The Laravel [service container](/docs/{{version}}/container) will automatically inject all dependencies that are type-hinted in this method's signature:
namespace App\Console\Commands;

<?php
use App\Models\User;
use App\Support\DripEmailer;
use Illuminate\Console\Command;

namespace App\Console\Commands;
class SendEmails extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mail:send {user}';

use App\Models\User;
use App\Support\DripEmailer;
use Illuminate\Console\Command;
/**
* The console command description.
*
* @var string
*/
protected $description = 'Send a marketing email to a user';

class SendEmails extends Command
/**
* Execute the console command.
*/
public function handle(DripEmailer $drip): void
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mail:send {user}';
$drip->send(User::find($this->argument('user')));
}
}
```

/**
* The console command description.
*
* @var string
*/
protected $description = 'Send a marketing email to a user';
```shell tab=Usage
php artisan mail:send 1
```

/**
* Execute the console command.
*/
public function handle(DripEmailer $drip): void
{
$drip->send(User::find($this->argument('user')));
}
}
<a name="command-structure"></a>
### Command Structure

After generating your command, you should define appropriate values for the `signature` and `description` properties of the class. These properties will be used when displaying your command on the `list` screen. The `signature` property also allows you to define [your command's input expectations](#defining-input-expectations). The `handle` method will be called when your command is executed. You may place your command logic in this method.

Let's take a look at an example command. Note that we are able to request any dependencies we need via the command's `handle` method. The Laravel [service container](/docs/{{version}}/container) will automatically inject all dependencies that are type-hinted in this method's signature.

> [!NOTE]
> For greater code reuse, it is good practice to keep your console commands light and let them defer to application services to accomplish their tasks. In the example above, note that we inject a service class to do the "heavy lifting" of sending the e-mails.
Expand Down Expand Up @@ -300,37 +306,41 @@ You may also make arguments optional or define default values for arguments:

Options, like arguments, are another form of user input. Options are prefixed by two hyphens (`--`) when they are provided via the command line. There are two types of options: those that receive a value and those that don't. Options that don't receive a value serve as a boolean "switch". Let's take a look at an example of this type of option:

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mail:send {user} {--queue}';

In this example, the `--queue` switch may be specified when calling the Artisan command. If the `--queue` switch is passed, the value of the option will be `true`. Otherwise, the value will be `false`:
```php tab=Definition filename=app/Console/Commands/SendEmails.php
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mail:send {user} {--queue}';
```

```shell
```shell tab=Usage
php artisan mail:send 1 --queue
```

In this example, the `--queue` switch may be specified when calling the Artisan command. If the `--queue` switch is passed, the value of the option will be `true`. Otherwise, the value will be `false`:

<a name="options-with-values"></a>
#### Options With Values

Next, let's take a look at an option that expects a value. If the user must specify a value for an option, you should suffix the option name with a `=` sign:

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mail:send {user} {--queue=}';

In this example, the user may pass a value for the option like so. If the option is not specified when invoking the command, its value will be `null`:
```php tab=Definition filename=app/Console/Commands/SendEmails.php
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mail:send {user} {--queue=}';
```

```shell
```shell tab=Usage
php artisan mail:send 1 --queue=default
```

In this example, the user may pass a value for the option like so. If the option is not specified when invoking the command, its value will be `null`.

You may assign default values to options by specifying the default value after the option name. If no option value is passed by the user, the default value will be used:

'mail:send {user} {--queue=default}'
Expand Down
58 changes: 29 additions & 29 deletions blade.md
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ If you would like to conditionally render your component, you may define a `shou

You may pass data to Blade components using HTML attributes. Hard-coded, primitive values may be passed to the component using simple HTML attribute strings. PHP expressions and variables should be passed to the component via attributes that use the `:` character as a prefix:

```blade
```blade tab=Usage
<x-alert type="error" :message="$message"/>
```

Expand Down Expand Up @@ -1855,22 +1855,22 @@ As you can see, we will chain the `format` method onto whatever expression is pa

If you attempt to "echo" an object using Blade, the object's `__toString` method will be invoked. The [`__toString`](https://www.php.net/manual/en/language.oop5.magic.php#object.tostring) method is one of PHP's built-in "magic methods". However, sometimes you may not have control over the `__toString` method of a given class, such as when the class that you are interacting with belongs to a third-party library.

In these cases, Blade allows you to register a custom echo handler for that particular type of object. To accomplish this, you should invoke Blade's `stringable` method. The `stringable` method accepts a closure. This closure should type-hint the type of object that it is responsible for rendering. Typically, the `stringable` method should be invoked within the `boot` method of your application's `AppServiceProvider` class:

use Illuminate\Support\Facades\Blade;
use Money\Money;
In these cases, Blade allows you to register a custom echo handler for that particular type of object. To accomplish this, you should invoke Blade's `stringable` method. The `stringable` method accepts a closure. This closure should type-hint the type of object that it is responsible for rendering. Typically, the `stringable` method should be invoked within the `boot` method of your application's `AppServiceProvider` class. Once your custom echo handler has been defined, you may simply echo the object in your Blade template:

/**
* Bootstrap any application services.
*/
public function boot(): void
{
Blade::stringable(function (Money $money) {
return $money->formatTo('en_GB');
});
}
```php tab=Definition filename=app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Blade;
use Money\Money;

Once your custom echo handler has been defined, you may simply echo the object in your Blade template:
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Blade::stringable(function (Money $money) {
return $money->formatTo('en_GB');
});
}
```

```blade
Cost: {{ $money }}
Expand All @@ -1879,23 +1879,23 @@ Cost: {{ $money }}
<a name="custom-if-statements"></a>
### Custom If Statements

Programming a custom directive is sometimes more complex than necessary when defining simple, custom conditional statements. For that reason, Blade provides a `Blade::if` method which allows you to quickly define custom conditional directives using closures. For example, let's define a custom conditional that checks the configured default "disk" for the application. We may do this in the `boot` method of our `AppServiceProvider`:
Programming a custom directive is sometimes more complex than necessary when defining simple, custom conditional statements. For that reason, Blade provides a `Blade::if` method which allows you to quickly define custom conditional directives using closures. For example, let's define a custom conditional that checks the configured default "disk" for the application. We may do this in the `boot` method of our `AppServiceProvider`. Once the custom conditional has been defined, you can use it within your templates:

use Illuminate\Support\Facades\Blade;

/**
* Bootstrap any application services.
*/
public function boot(): void
{
Blade::if('disk', function (string $value) {
return config('filesystems.default') === $value;
});
}
```php tab=Definition filename=app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Blade;

Once the custom conditional has been defined, you can use it within your templates:
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Blade::if('disk', function (string $value) {
return config('filesystems.default') === $value;
});
}
```

```blade
```blade tab=Usage
@disk('local')
<!-- The application is using the local disk... -->
@elsedisk('s3')
Expand Down
62 changes: 33 additions & 29 deletions broadcasting.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ You can find detailed Reverb installation and usage instructions in the [Reverb
<a name="pusher-channels"></a>
### Pusher Channels

If you plan to broadcast your events using [Pusher Channels](https://pusher.com/channels), you should install the Pusher Channels PHP SDK using the Composer package manager:
If you plan to broadcast your events using [Pusher Channels](https://pusher.com/channels), you should install the Pusher Channels PHP SDK using the Composer package manager.

```shell
Next, you should configure your Pusher Channels credentials in the `config/broadcasting.php` configuration file. An example Pusher Channels configuration is already included in this file, allowing you to quickly specify your key, secret, and application ID. Typically, you should configure your Pusher Channels credentials in your application's `.env` file.

```shell tab=Installation
composer require pusher/pusher-php-server
```

Next, you should configure your Pusher Channels credentials in the `config/broadcasting.php` configuration file. An example Pusher Channels configuration is already included in this file, allowing you to quickly specify your key, secret, and application ID. Typically, you should configure your Pusher Channels credentials in your application's `.env` file:

```ini
```ini tab=Configuration filename=.env
PUSHER_APP_ID="your-pusher-app-id"
PUSHER_APP_KEY="your-pusher-key"
PUSHER_APP_SECRET="your-pusher-secret"
Expand Down Expand Up @@ -646,42 +646,46 @@ Private and presence broadcast channels authenticate the current user via your a
<a name="defining-channel-classes"></a>
### Defining Channel Classes

If your application is consuming many different channels, your `routes/channels.php` file could become bulky. So, instead of using closures to authorize channels, you may use channel classes. To generate a channel class, use the `make:channel` Artisan command. This command will place a new channel class in the `App/Broadcasting` directory.
If your application is consuming many different channels, your `routes/channels.php` file could become bulky. So, instead of using closures to authorize channels, you may use channel classes.

```shell
1. To generate a channel class, use the `make:channel` Artisan command. This command will place a new channel class in the `App/Broadcasting` directory.
2. Next, register your channel in your `routes/channels.php` file.
3. Finally, you may place the authorization logic for your channel in the channel class' `join` method. This `join` method will house the same logic you would have typically placed in your channel authorization closure. You may also take advantage of channel model binding:

```shell tab=Creation
php artisan make:channel OrderChannel
```

Next, register your channel in your `routes/channels.php` file:

use App\Broadcasting\OrderChannel;
```php tab=Registration filename=routes/channels.php
use App\Broadcasting\OrderChannel;

Broadcast::channel('orders.{order}', OrderChannel::class);
Broadcast::channel('orders.{order}', OrderChannel::class);
```

Finally, you may place the authorization logic for your channel in the channel class' `join` method. This `join` method will house the same logic you would have typically placed in your channel authorization closure. You may also take advantage of channel model binding:
```php tab=Usage filename=app/Broadcasting/OrderChannel.php
<?php

<?php
namespace App\Broadcasting;

namespace App\Broadcasting;
use App\Models\Order;
use App\Models\User;

use App\Models\Order;
use App\Models\User;
class OrderChannel
{
/**
* Create a new channel instance.
*/
public function __construct() {}

class OrderChannel
/**
* Authenticate the user's access to the channel.
*/
public function join(User $user, Order $order): array|bool
{
/**
* Create a new channel instance.
*/
public function __construct() {}

/**
* Authenticate the user's access to the channel.
*/
public function join(User $user, Order $order): array|bool
{
return $user->id === $order->user_id;
}
return $user->id === $order->user_id;
}
}
```

> [!NOTE]
> Like many other classes in Laravel, channel classes will automatically be resolved by the [service container](/docs/{{version}}/container). So, you may type-hint any dependencies required by your channel in its constructor.
Expand Down
8 changes: 3 additions & 5 deletions cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,13 @@ This table should also have a string partition key with a name that corresponds

Typically, DynamoDB will not proactively remove expired items from a table. Therefore, you should [enable Time to Live (TTL)](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/TTL.html) on the table. When configuring the table's TTL settings, you should set the TTL attribute name to `expires_at`.

Next, install the AWS SDK so that your Laravel application can communicate with DynamoDB:
Next, install the AWS SDK so that your Laravel application can communicate with DynamoDB. In addition, you should ensure that values are provided for the DynamoDB cache store configuration options. Typically these options, such as `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`, should be defined in your application's `.env` configuration file:

```shell
```shell tab=Installation
composer require aws/aws-sdk-php
```

In addition, you should ensure that values are provided for the DynamoDB cache store configuration options. Typically these options, such as `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`, should be defined in your application's `.env` configuration file:

```php
```php tab=Configuration filename=config/cache.php
'dynamodb' => [
'driver' => 'dynamodb',
'key' => env('AWS_ACCESS_KEY_ID'),
Expand Down
Loading