php artisan command,laravel下command执行php脚本!

一、创建:

php artisan make:command sync#创建脚本

php artisan command:sync #执行脚本

php artisan command:方法 参数【--参数名=参数值】

php artisan command:sync file #执行带参数的脚本

php artisan command:sync --file #执行带参数的脚本

ctrl + C #撤销执行

二、修改kernel.php,引入创建

133c5ddf1558

image.png

三、修改sync.php

主要代码:

protected $signature = 'command:sync {param?} {--func=}';

//command:name 改成文件名

//{param?} 可选参数,不用指定参数名

//{--func=} --func=参数名 ,指定参数名

public function handle(){

//默认调用此方法

}

demo示例:

namespace App\Console\Commands;

use Illuminate\Console\Command;

use Illuminate\Support\Facades\DB;

set_time_limit(0); //解除PHP脚本时间30s限制

ini_set('memory_limit','128M');//修改内存值

class sync extends Command

{

/**

* The name and signature of the console command.

*

* @var string

*/

protected $signature = 'command:sync {param?} {--func=}';

/**

* The console command description.

*

* @var string

*/

protected $description = 'Command description: 调用示例,联系人同步:

php artisan command:sync contacts or php artisan command:sync --func=contacts ';

/**

* Create a new command instance.

*

* @return void

*/

public function __construct()

{

parent::__construct();

}

/**

* Execute the console command.

*

* @return mixed

*/

public function handle()

{

// 入口方法

$param = $this->argument('param'); // argument不指定参数名的情况下用 file

$func = $this->option('func'); // option用--开头指定参数名 --func=file

$method = isset($param) ? $param : $func;//兼容两种传参方式调用

//本类中是否存在传来的方法

if(!method_exists(new self,$method)){

echo '不存在的方法,请确认输入是否有误!';

}else{

self::$method();

}

}

//同步

public static function index(){

$s = 0;//从0开始

$e = 10; //取10条数据

while(true){//死循环模拟分页执行代码

if($s >=100){//满足条件跳出死循环

exit('success');

}

//执行代码

file_put_contents('./test.log',$s.'--'.$e."\n",FILE_APPEND);

$s = $s+$e; //下次分页开始

}

}

//文件同步

public static function file(){

echo"文件同步";

}

//会员同步

public static function file(){

echo"会员同步";

}

//联系人同步

public static function contacts(){

echo "联系人同步";

}

}

index执行结果:php artisan command:sync index (php artisan command:sync --func=index)

133c5ddf1558

image.png

133c5ddf1558

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值