我正在开发一个带有Laravel 5.6的博客平台,可以安排帖子的发布。 为了实现它,我:
- 通过放置在
Console/Commands
文件夹中PostPublish
类创建了post:publish {id}
命令。 - 根据文档所说的,该命令会自动加载,因为在
Console/Commands
中,所以我没有在Console/Kernel.php
中注册到$commands
属性(如果我运行php artisan list
,post:publish
确实被列出(。 - 每当用户安排帖子时,该帖子都会插入到分类为
scheduled
的blog_posts
表中,并将新记录添加到tasks
表中(id
、command
、cron
(。4
、post:publish 56
、17 11 18 9 2
。 - 我在服务器上添加了 Laravel 的调度程序 Cron 条目以每分钟运行一次,将
>> /dev/null 2>&1
替换为日志文件的路径:* * * * * /opt/alt/php71/usr/bin/php /home/qs266dg7/public_html && php artisan schedule:run >> /home/qs266dg7/public_html/app/Console/cron.log
- 为了动态设置 cronjob,我决定目前使用此解决方案(尽管我认为不是最有效的解决方案(。因此,在
Kernel{}
类内的schedule()
方法中,我获取tasks
表的所有条目,每个条目都运行command()
方法。
问题:帖子的状态不会像PostPublish
类的方法中所述handle()
改变,我不知道如何理解问题所在,因为一切都是在服务器上自动完成的。 拉拉维尔的appendOutputTo()
在cron.log
中没有产生任何输出,这就是我设置>> /home/qs266dg7/public_html/app/Console/cron.log
的原因。日志文件现在包含的是一长串:
Status: 404 Not Found
X-Powered-By: PHP/7.1.20
Content-type: text/html; charset=UTF-8
No input file specified.
问题出在哪里?在命令中?在执行它?在克龙?我怎么知道?
应用程序/控制台/命令/发布后.php
class PostPublish extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'post:publish {id}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Publish posts that have been scheduled';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$post = BlogPost::find($this->argument('id'));
$post->status = "published";
$post->published_at = $post->scheduled_at;
$post->scheduled_at = null;
return $post->save();
}
}
应用程序/控制台/内核.php
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
//
];
/**
* Define the application's command schedule.
*
* @param IlluminateConsoleSchedulingSchedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$tasks = Task::all();
foreach ($tasks as $task) {
$schedule->command($task->command)
->cron($task->cron)
->appendOutputTo('/home/qs266dg7/public_html/storage/app/Console/cron.log');
}
}
/**
* Register the commands for the application.
*
* @return void
*/
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
您可以通过创建日志进行检查
use Log;
class PostPublish extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'post:publish {id}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Publish posts that have been scheduled';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
try{
$post = BlogPost::find($this->argument('id'));
$post->status = "published";
$post->published_at = $post->scheduled_at;
$post->scheduled_at = null;
return $post->save();
}
catch (Exception $e) {
Log::alert($e);
}
}
}
您可以在此文件中检查您的登录
storage/logs/laravel.log