多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# laravel自定义指令生成token 生成基础对象指令 ~~~ php artisan make:command GetToken ~~~ 在app\Console\Commands\目录下可以找到GetToken, 如果你之前没有执行过这个指令,是无法找到Commands目录的。 具体编写如下: > User是用户模型,映射用户表 > $signature 是用来生成指令的名字 > $description 是用来查看指令的时候的描述,说明本指令是用来做啥的 > handle手柄函数,编写本指令的具体逻辑 ~~~ <?php namespace App\Console\Commands; use Illuminate\Console\Command; use App\Models\User; use Illuminate\Support\Facades\Auth; class GetToken extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'cms:get-token'; /** * The console command description. * * @var string */ protected $description = '快速生成token'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { // $userId = $this->ask("请输入用户id"); $user = User::find($userId); if(!$user) { return $this->error("用户不存在"); } $ttl = 365*24*60; $this->info(Auth::guard('api')->setTTL($ttl)->fromUser($user)); } } ~~~ 使用方法: ~~~ php artisan cms:get-token ~~~