laravel\app\console\command怎么用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了laravel\app\console\command怎么用相关的知识,希望对你有一定的参考价值。
参考技术A 场景比如使用redis作为计数器,然后在每天的凌晨将计数保存到mysql
比如定时执行一些批量的任务
扩展artisan的command
例子:批量将文字题目生成为图片
artisan command:make QuestionCommand
# app/command/QuestionCommand.php
生成php文件之后,首先需要修改name为你的命令
protected $name = 'question:gen';
getArguments, getOptions 分别为获取参数和选项的值
getArguments // command arg1 arg2
getOptions // command --opt1=val1 --opt2=val2
fire // 执行命令的时候具体要做的事情
较为完整的例子
<?php
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class QuestionCommand extends Command
/**
* The console command name.
*
* @var string
*/
protected $name = 'question:gen';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成question图片';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
parent::__construct();
/**
* Execute the console command.
*
* @return void
*/
public function fire()
$questions = $this->option('all') ? Question::all() : Question::whereThumb(0)->get();
// 初始化markdown解析引擎
$ciconia = new Ciconia\Ciconia();
$ciconia->addExtension(new Ciconia\Extension\Gfm\FencedCodeBlockExtension());
$ciconia->addExtension(new Ciconia\Extension\Gfm\TaskListExtension());
$ciconia->addExtension(new Ciconia\Extension\Gfm\InlineStyleExtension());
$ciconia->addExtension(new Ciconia\Extension\Gfm\WhiteSpaceExtension());
$ciconia->addExtension(new Ciconia\Extension\Gfm\TableExtension());
foreach ($questions as $question)
$item = array(
'id' => $question->id,
'absPath' => public_path('upload/question/' . $question->id . '.png'),
'relPath' => 'question/' . $question->id . '.png',
'question' => $ciconia->render($question->question),
);
Queue::push('code2png', $item, 'code2png');
$this->info('Question ' . $question->id . ' has been added to the queue.');
/**
* Get the console command arguments.
*
* @return array
*/
protected function getArguments()
return array(
// array('example', InputArgument::REQUIRED, 'An example argument.'),
);
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
return array(
array('all', null, InputOption::VALUE_OPTIONAL, '全部重新生成', null),
);
需要在app/start/artisan.php里面添加下面一行才会生效
// 4.0
$artisan->add(new QuestionCommand);
// 4.1
Artisan::add(new QuestionCommand);
命令行中这样执行
artisan question:gen --all=1
如果要在Controller里面调用,可以这样
Artisan::call('question:gen', array('--all' => 1));本回答被提问者采纳
如何将 App\Exceptions 从 laravel 7 升级到 laravel 8
【中文标题】如何将 App\\Exceptions 从 laravel 7 升级到 laravel 8【英文标题】:How to upgrade App\Exceptions from laravel 7 to laravel 8如何将 App\Exceptions 从 laravel 7 升级到 laravel 8 【发布时间】:2021-10-09 20:24:08 【问题描述】:如何升级 App\Exceptions;从 laravel 7 到 laravel 8
public function render($request, Throwable $exception)
if ($exception instanceof UnauthorizedException)
if (Auth::user() instanceof Admin)
return redirect()->route('dashboard');
return redirect()->route('home');
return parent::render($request, $exception);
【问题讨论】:
来自文档laravel.com/docs/8.x/errors#rendering-exceptions 【参考方案1】:请更改您的app/Exceptions/Handler.php
,如下所示
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
/**
* A list of the exception types that are not reported.
*
* @var array
*/
protected $dontReport = [
//
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
$this->reportable(function (Throwable $e)
//
);
【讨论】:
以上是关于laravel\app\console\command怎么用的主要内容,如果未能解决你的问题,请参考以下文章