Installation:
composer require maknz/slack
Ensure your project meets the PHP version requirements (5.5+).
Slack Setup:
https://hooks.slack.com/services/...).First Message:
use Maknz\Slack\Client;
$client = new Client('YOUR_WEBHOOK_URL');
$client->send('Hello, Slack!', 'General');
YOUR_WEBHOOK_URL with your actual webhook URL.#general, @user).Laravel Integration (if applicable):
composer require maknz/slack-laravel
php artisan vendor:publish --provider="Maknz\Slack\SlackServiceProvider"
.env with your webhook URL:
SLACK_WEBHOOK_URL=https://hooks.slack.com/...
$client->send('Deployment complete!', '#deployments');
$attachment = [
'title' => 'Error Logged',
'text' => 'Failed to process order #12345',
'color' => '#FF0000',
'fields' => [
['title' => 'Order ID', 'value' => '12345', 'short' => true],
['title' => 'User', 'value' => 'john.doe', 'short' => true],
],
];
$client->send('Order Processing Error', '#errors', null, [$attachment]);
$attachment = [
'title' => 'Approve Deployment?',
'text' => 'Click below to confirm:',
'callback_id' => 'deploy_confirm',
'actions' => [
['type' => 'button', 'text' => 'Approve', 'value' => 'approve', 'style' => 'primary'],
['type' => 'button', 'text' => 'Cancel', 'value' => 'cancel'],
],
];
$client->send('Deployment Request', '#deployments', null, [$attachment]);
$channel = config('slack.channels.alerts'); // From Laravel config
$client->send('System Alert', $channel);
try {
$client->send('Critical Error', '#errors');
} catch (\Maknz\Slack\Exception\SlackException $e) {
Log::error('Slack notification failed: ' . $e->getMessage());
}
foreach ($failedJobs as $job) {
$client->send(
"Job {$job->id} failed: {$job->error}",
'#job-failures'
);
}
Service Container Binding: Bind the client in a service provider for dependency injection:
$this->app->singleton('slack', function ($app) {
return new Client(config('slack.webhook_url'));
});
Use it in controllers:
public function __construct(\Maknz\Slack\Client $slack) {
$this->slack = $slack;
}
Events and Listeners: Trigger Slack notifications from Laravel events:
// In an event listener
$this->slack->send('User registered: ' . $user->email, '#new-users');
NexySlackBundle for dependency injection and configuration:
# config.yml
nexy_slack:
webhook_url: '%slack_webhook_url%'
Inject the client in services:
use Nexy\SlackBundle\Client\SlackClientInterface;
public function __construct(SlackClientInterface $slack) { ... }
class SlackNotifier {
protected $client;
public function __construct(Client $client) {
$this->client = $client;
}
public function alert($message, $channel, array $context = []) {
$attachment = [
'title' => 'Alert',
'text' => $message,
'fields' => $this->formatContext($context),
];
$this->client->send('ALERT', $channel, null, [$attachment]);
}
protected function formatContext(array $context) {
// Custom logic to format context data
}
}
Deprecation Warning:
slack/slack-webhook-php (official, actively maintained).spatie/slack-notification-channel (for Laravel).Attachment Format Changes:
mrkdwn for markdown) may not work as expected. Test thoroughly.Rate Limiting:
Webhook URL Exposure:
.env in Laravel).Error Handling:
Maknz\Slack\Exception\SlackException for HTTP errors (e.g., invalid webhook). Catch and log these gracefully.Enable Debugging: The package uses Guzzle under the hood. Enable Guzzle’s debug mode to inspect requests:
$client = new Client($webhookUrl);
$client->getHttpClient()->setDebug(true); // Requires Guzzle 6+
Log the debug output to inspect payloads:
$client->send('Test', '#test');
// Check Guzzle's debug output for the raw request payload
Validate Payloads: Use Slack’s webhook tester to verify your messages render correctly.
Laravel Config:
The Laravel wrapper expects a slack config key:
// config/slack.php
return [
'webhook_url' => env('SLACK_WEBHOOK_URL'),
'channels' => [
'alerts' => '#alerts',
'deployments' => '#deployments',
],
];
Default Channel:
If no channel is provided, the package defaults to #general. Override this in the constructor:
$client = new Client($webhookUrl, ['default_channel' => '#custom-channel']);
Custom HTTP Client:
Replace the default HTTP client (Guzzle) by extending the Client class:
use Maknz\Slack\Client as BaseClient;
use GuzzleHttp\Client as GuzzleClient;
class CustomSlackClient extends BaseClient {
public function __construct($webhookUrl, array $options = []) {
$this->setHttpClient(new GuzzleClient($options));
parent::__construct($webhookUrl, $options);
}
}
Message Formatting: Create a decorator to transform messages before sending:
class SlackMessageDecorator {
protected $client;
public function __construct(Client $client) {
$this->client = $client;
}
public function send($message, $channel, array $attachments = []) {
$formattedMessage =
How can I help you explore Laravel packages today?