You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Laravel Envoy任务传递参数?优化Docker-Compose环境命令

Great question! You’re absolutely right to want to streamline your Envoy setup—having a bloated Envoy.blade.php with duplicate tasks is a pain, and memorizing all those task names isn’t sustainable. The good news is Laravel Envoy fully supports passing dynamic parameters to tasks, which lets you create flexible, reusable tasks instead of one-off ones. Here’s how to implement this for your Composer and Artisan workflows:

1. Create a Reusable Composer Task

Instead of separate tasks for composer-install, composer-require, etc., make a single task that accepts parameters for the action (install, require, remove, update) and optional package name.

Add this to your Envoy.blade.php:

@task('composer', ['on' => 'local'])
    # Use Docker Compose to run the Composer command with dynamic parameters
    docker-compose exec app composer {{ $action ?? 'install' }} {{ $package ?? '' }}
@endtask

How to Use It:

  • Run a full Composer install (default action if no --action is passed):
    envoy run composer
    
  • Require a specific package:
    envoy run composer --action=require --package=xxx/yyy
    
  • Remove a package:
    envoy run composer --action=remove --package=xxx/yyy
    
  • Update all dependencies:
    envoy run composer --action=update
    

2. Build a Universal Artisan Task

Do the same for Artisan commands to avoid creating tasks for migrate, cache:clear, db:seed, etc. Add this to your Envoy file:

@task('artisan', ['on' => 'local'])
    docker-compose exec app php artisan {{ $command }} {{ $params ?? '' }}
@endtask

How to Use It:

  • Run database migrations with seeding:
    envoy run artisan --command=migrate --params='--seed'
    
  • Clear application cache:
    envoy run artisan --command=cache:clear
    
  • Generate a model with migration:
    envoy run artisan --command=make:model --params='Post -m'
    

3. Bonus: Add Parameter Validation (Optional)

If you want to enforce required parameters (like making sure --action is provided for non-default Composer commands), you can add a quick check in the task:

@task('composer', ['on' => 'local'])
    @if(empty($action) && !empty($package))
        @error('You must specify an action (require/remove) when passing a package!')
    @endif

    docker-compose exec app composer {{ $action ?? 'install' }} {{ $package ?? '' }}
@endtask

This way, you’ll get a clear error if someone tries to pass a package without an action, preventing mistakes.

Why This Works

By using dynamic parameters, you’re turning Envoy from a list of rigid tasks into a flexible tool that adapts to your needs. Your Envoy.blade.php will stay lean, and you only need to remember two core task names (composer and artisan) instead of dozens of one-off ones.

内容的提问来源于stack exchange,提问作者Ivan Vilanculo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:07:57