laravel-console artisan

11.x-dev
版本
2025-02-08
版本更新時(shí)間
675
安裝
5
star
webman-tech/laravel-console
Laravel illuminate/console for webman
為什么會(huì)有這個(gè)擴(kuò)展
官方的 webman/console 很便捷也很實(shí)用,而且很貼近 webman 框架,這是事實(shí)
以下幾個(gè)原因是本擴(kuò)展誕生的原因:
- webman/console 是基于 symfony/console 的,比較底層,illuminate/console 同樣也是,但是在其上層封裝了一層,使用起來(lái)更加方便
- 給用慣了 laravel 的人一個(gè)熟悉的環(huán)境(尤其是在建一個(gè) command 時(shí),laravel 的 command 使用 signature 定義參數(shù)更加方便)
- 現(xiàn)在有很多實(shí)用的第三方擴(kuò)展是基于 laravel,可能遷移其中的組件時(shí)能夠比較方便,但是其中的 command 無(wú)法直接使用,因此本擴(kuò)展可以讓這些擴(kuò)展的 command 直接使用
介紹
所有方法和配置與 laravel 幾乎一模一樣,因此使用方式完全參考 Laravel文檔 即可
注意:只是用法一致,但不帶有任何 laravel 框架自帶的命令
安裝
composer require webman-tech/laravel-console
如果需要支持 webman/console 的命令,直接安裝即可
composer require webman/console
配置
配置文件:config/plugin/webman-tech/laravel-console/artisan.php
命令掃描
默認(rèn)自動(dòng)掃描 app\command
下的命令(同 webman/console 的邏輯)
如果安裝了 webman/console
,會(huì)自動(dòng)掃描其命令(完全兼容)
如果需要自定義命令掃描目錄,可以在 commands_path
中添加,如:
return [
'commands_path' => [
app_path() . '/myCommands' => 'app\myCommands',
],
];
自定義命令
當(dāng)需要添加來(lái)自第三方包,或者沒(méi)有放在掃描目錄中的命令時(shí),可以在 commands
中添加,如:
return [
'commands' => [
\App\Commands\MyCommand::class,
],
];
使用
- 新建命令
namespace app\command;
use Illuminate\Console\Command;
class SendEmails extends Command
{
protected $signature = 'mail:send {userId}';
protected $description = 'Send a marketing email to a user';
public function handle(Mailer $mailer): void
{
$mailer->send(User::find($this->argument('userId')));
}
}
- 注冊(cè)命令
由于上面的命令是放在 app\command
目錄下的,因此不需要注冊(cè),
如果是放在其他目錄下的,需要在配置中添加
- 調(diào)用
命令行調(diào)用
php artisan mail:send 1
業(yè)務(wù)中調(diào)用(比如控制器)
use \WebmanTech\LaravelConsole\Facades\Artisan;
Artisan::call('mail:send', ['userId' => 1]);