Get "PHP 8 in a Nuthshell" (Now with PHP 8.4)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

How to use .env values in Laravel Envoy

Laravel Envoy is a great tool if you want to create deployment flow for your application or even something as small as writing a script to pull the latest code and deploy the application right away.

One of the cool features of Envoy is it can also send a notification to different services after each task is executed such as Slack, Discord, Telegram, and so on. So, for instance, if you want to send a notification to a Slack channel, you could do it using the @slack directive like so.

@finished
    @slack('webhook-url', '#bots')
@endfinished

Here, webhook-url is the webhook URL for Slack that you can get by creating an “Incoming WebHooks” integration in your Slack control panel.

As you can see, you’d need to feed the plain webhook URL to the @slack directive which in my opinion is not a best practice to follow as your webhook URL falls into the category of sensitive data.

So, what you do to alternatively is define a variable for webhook URL in your application’s .env file and use that into the Envoy.blade.php. But here’s a thing. By default, Envoy can read .env file.

If you’ve defined a variable for webhook URL in the .env like so.

SLACK_WEBHOOK=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX

You can access it in the Envoy by pulling it in @setup like so.

@setup
require __DIR__.'/vendor/autoload.php';
(new \Dotenv())->load(__DIR__, '.env');
@endsetup

@finished
    @slack(env('SLACK_WEBHOOK'), '#bots')
@endfinished

And that is how you can access just about any .env values in your Envoy script!

Learn the fundamentals of PHP 8 (including 8.1, 8.2, 8.3, and 8.4), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article?

Buy me a coffee

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?